-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
52 lines (43 loc) · 1.5 KB
/
auth.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
package globus
import (
"context"
"fmt"
"net/http"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)
const authBaseUrl = "https://auth.globus.org/v2"
// Returns a two-legged (client credental) http client with oauth2 authentication.
// The function can fail if the token acquisition check fails.
func AuthCreateServiceClient(ctx context.Context, clientID string, clientSecret string, scopes []string) (client GlobusClient, err error) {
conf := clientcredentials.Config{
ClientID: clientID,
ClientSecret: clientSecret,
TokenURL: authBaseUrl + "/oauth2/token",
Scopes: scopes,
}
// token acquisition check
_, tokenError := conf.Token(ctx)
if tokenError != nil {
return GlobusClient{}, fmt.Errorf("error getting token for client: %s", tokenError.Error())
}
return GlobusClient{client: conf.Client(ctx)}, nil
}
func HttpClientToGlobusClient(client *http.Client) GlobusClient {
return GlobusClient{client: client}
}
// This is a very basic function that returns an oauth2 config
// with the token url hard-coded to the one provided by Globus.
func AuthGenerateOauthClientConfig(ctx context.Context, clientID string, clientSecret string, redirectURL string, scopes []string) (conf oauth2.Config) {
conf = oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
Endpoint: oauth2.Endpoint{
TokenURL: authBaseUrl + "/oauth2/token",
AuthURL: "https://auth.globus.org/v2/oauth2/authorize",
},
RedirectURL: redirectURL,
Scopes: scopes,
}
return conf
}