-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaps.go
128 lines (111 loc) · 2.88 KB
/
aps.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package aps
import (
"net/http"
"strings"
"github.com/markbates/goth"
"golang.org/x/oauth2"
)
const (
authURL string = "http://localhost:9096/authorize"
tokenURL string = "http://localhost:9096/token"
endpointProfile string = "http://localhost:9096/userinfo"
)
// Provider is the implementation of `goth.Provider` for accessing APS.
type Provider struct {
ClientKey string
Secret string
CallbackURL string
config *oauth2.Config
prompt oauth2.AuthCodeOption
}
// New - Please fill the code
func New(clientKey, secret, callbackURL string, scopes ...string) *Provider {
p := &Provider{
ClientKey: clientKey,
Secret: secret,
CallbackURL: callbackURL,
}
p.config = createConfig(p, scopes)
return p
}
func (p *Provider) Client() *http.Client {
return http.DefaultClient
}
func createConfig(p *Provider, scopes []string) *oauth2.Config {
configuration := &oauth2.Config{
ClientID: p.ClientKey,
ClientSecret: p.Secret,
RedirectURL: p.CallbackURL,
Endpoint: oauth2.Endpoint{
AuthURL: authURL,
TokenURL: tokenURL,
},
Scopes: []string{},
}
if len(scopes) > 0 {
for _, scope := range scopes {
configuration.Scopes = append(configuration.Scopes, scope)
}
}
return configuration
}
// FetchUser - Please fill the code
func (p *Provider) FetchUser(session goth.Session) (goth.User, error) {
s := session.(*Session)
user := goth.User{
AccessToken: s.AccessToken,
Provider: p.Name(),
RefreshToken: s.RefreshToken,
ExpiresAt: s.ExpiresAt,
}
req, err := http.NewRequest("GET", endpointProfile, nil)
req.Header.Set("Authorization", "Bearer "+s.AccessToken)
resp, err := p.Client().Do(req)
if err != nil {
if resp != nil {
resp.Body.Close()
}
return user, err
}
defer resp.Body.Close()
return user, err
}
// RefreshToken - Please fill the code
func (p *Provider) RefreshToken(refreshToken string) (*oauth2.Token, error) {
token := &oauth2.Token{RefreshToken: refreshToken}
ts := p.config.TokenSource(oauth2.NoContext, token)
newToken, err := ts.Token()
if err != nil {
return nil, err
}
return newToken, err
}
// RefreshTokenAvailable - Please fill the code
func (p *Provider) RefreshTokenAvailable() bool {
return true
}
// Name is the name used to retrieve this provider later.
func (p *Provider) Name() string {
return "aps"
}
// Debug is a no-op for the APS package.
func (p *Provider) Debug(debug bool) {}
// BeginAuth - Please fill the code
func (p *Provider) BeginAuth(state string) (goth.Session, error) {
var opts []oauth2.AuthCodeOption
if p.prompt != nil {
opts = append(opts, p.prompt)
}
url := p.config.AuthCodeURL(state, opts...)
session := &Session{
AuthURL: url,
}
return session, nil
}
// SetPrompt - Please fill the code
func (p *Provider) SetPrompt(prompt ...string) {
if len(prompt) == 0 {
return
}
p.prompt = oauth2.SetAuthURLParam("prompt", strings.Join(prompt, " "))
}