This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
oauth2.go
103 lines (88 loc) · 2.77 KB
/
oauth2.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package factorial
import (
"context"
"net/http"
"golang.org/x/oauth2"
)
const (
authURL = "https://api.factorialhr.com/oauth/authorize"
tokenURL = "https://api.factorialhr.com/oauth/token"
)
// OAuthProvider keep the basic information
// needed for create and keep a connection
// using OAuth2
type OAuthProvider struct {
conf *oauth2.Config
ctx context.Context
}
// OAuthProviderOption defines an option for a OAuthProvider.
type OAuthProviderOption func(*OAuthProvider)
// NewOAuthProvider will create a new OAuthProvider applying
// the given options
func NewOAuthProvider(opts ...OAuthProviderOption) *OAuthProvider {
provider := &OAuthProvider{
conf: &oauth2.Config{
Endpoint: oauth2.Endpoint{
AuthURL: authURL,
TokenURL: tokenURL,
},
},
ctx: context.Background(),
}
for _, opt := range opts {
opt(provider)
}
return provider
}
// WithClientID will setup a new clientID for the OAuth2 config
func WithClientID(clientID string) OAuthProviderOption {
return func(o *OAuthProvider) {
o.conf.ClientID = clientID
}
}
// WithClientSecret will setup a new client secret for the OAuth2 config
func WithClientSecret(clientSecret string) OAuthProviderOption {
return func(o *OAuthProvider) {
o.conf.ClientSecret = clientSecret
}
}
// WithScopes will setup the scopes that the OAuth2 should ask
func WithScopes(scopes []string) OAuthProviderOption {
return func(o *OAuthProvider) {
o.conf.Scopes = scopes
}
}
// WithRedirectURL will setup the redirect url needed for OAuth2
func WithRedirectURL(redirectURL string) OAuthProviderOption {
return func(o *OAuthProvider) {
o.conf.RedirectURL = redirectURL
}
}
// GetAuthURL will return the return the url for redirect
// and start the OAuth2 process
func (o OAuthProvider) GetAuthURL(state string) string {
return o.conf.AuthCodeURL(state)
}
// GetTokenFromCode method will find the token with the given code, this method
// should be called after a success callback received from auth process
func (o OAuthProvider) GetTokenFromCode(code string) (*oauth2.Token, error) {
return o.conf.Exchange(o.ctx, code)
}
// RefreshToken method will refresh the given token
func (o OAuthProvider) RefreshToken(t *oauth2.Token) (*oauth2.Token, error) {
return o.conf.TokenSource(o.ctx, t).Token()
}
// Client method will return a new http.Client for use in our calls, using
// the TokenSource will even refresh the token if needed
func (o OAuthProvider) Client(t *oauth2.Token) *http.Client {
return o.conf.Client(o.ctx, t)
}
// ClientWithSource will return a new http.Client for us in our calls, this
// method receives a custom tokenSource that can be used for persist our token
func (o OAuthProvider) ClientWithSource(s oauth2.TokenSource) *http.Client {
return &http.Client{
Transport: &oauth2.Transport{
Source: s,
},
}
}