forked from kothar/asana-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth.go
56 lines (47 loc) · 1.28 KB
/
oauth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package asana
import (
"context"
"golang.org/x/oauth2"
)
var defaultOAuthEndpoint = oauth2.Endpoint{
AuthURL: "https://app.asana.com/-/oauth_authorize",
TokenURL: "https://app.asana.com/-/oauth_token",
}
// AppConfig provides the details needed to authenticate users with
// Asana on behalf of an Asana client application
type AppConfig struct {
ClientID string
ClientSecret string
RedirectURL string
}
// App represents an Asana client application
type App struct {
config *oauth2.Config
}
// NewApp creates a new App with the provided configuration
func NewApp(config *AppConfig) *App {
return &App{
config: &oauth2.Config{
ClientID: config.ClientID,
ClientSecret: config.ClientSecret,
Endpoint: defaultOAuthEndpoint,
RedirectURL: config.RedirectURL,
Scopes: nil,
},
}
}
// see oauth2 package
func (a *App) AuthCodeURL(state string) string {
return a.config.AuthCodeURL(state)
}
// see oauth2 package
func (a *App) Exchange(code string) (*oauth2.Token, error) {
ctx := context.Background()
return a.config.Exchange(ctx, code)
}
// NewClient creates a new Asana client using the provided credentials
func (a *App) NewClient(token *oauth2.Token) *Client {
ctx := context.Background()
client := a.config.Client(ctx, token)
return NewClient(client)
}