-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsso.go
82 lines (69 loc) · 2.02 KB
/
sso.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package evegoesi
import (
"context"
"errors"
"golang.org/x/oauth2"
"net/http"
"sync"
)
const (
authURL = "https://login.eveonline.com/v2/oauth/authorize"
tokenURL = "https://login.eveonline.com/v2/oauth/token"
)
var (
errParam = errors.New("missing params")
)
type SSOAuthenticator struct {
scopeLock sync.Mutex
HttpClient *http.Client
OauthConfig *oauth2.Config
Token *oauth2.Token
Code string
Error error
}
func NewSSOAuthenticatorV2(clientID string, clientSecret string, redirectURL string, scopes []string) (sa *SSOAuthenticator, err error) {
if clientID == "" || clientSecret == "" || redirectURL == "" {
return nil, errParam
}
sa = &SSOAuthenticator{}
sa.OauthConfig = &oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
RedirectURL: redirectURL,
Scopes: scopes,
Endpoint: oauth2.Endpoint{AuthURL: authURL, TokenURL: tokenURL},
}
return sa, nil
}
func (sa *SSOAuthenticator) AuthorizeURL(state string, onlineAccess bool) (url string) {
// Generate the URL
if onlineAccess == true {
url = sa.OauthConfig.AuthCodeURL(state, oauth2.AccessTypeOnline)
} else {
url = sa.OauthConfig.AuthCodeURL(state, oauth2.AccessTypeOffline)
}
return
}
func (sa *SSOAuthenticator) InitWithRefreshToken(refreshToken string) (res *SSOAuthenticator) {
res = sa
token := &oauth2.Token{RefreshToken: refreshToken}
tokenSource := res.OauthConfig.TokenSource(context.Background(), token)
oauth2.ReuseTokenSource(token, tokenSource)
res.HttpClient = oauth2.NewClient(context.Background(), tokenSource)
res.Token = token
return
}
func (sa *SSOAuthenticator) InitWithCode(code string) (res *SSOAuthenticator) {
var err1, err2 error
res = sa
res.Code = code
res.Token, err1 = res.OauthConfig.Exchange(context.Background(), res.Code)
res.HttpClient = sa.OauthConfig.Client(context.Background(), sa.Token)
res.Token, err2 = sa.OauthConfig.TokenSource(context.Background(), sa.Token).Token()
if err1 != nil {
res.Error = err1
} else if err2 != nil {
res.Error = err2
}
return
}