-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.go
47 lines (39 loc) · 1.22 KB
/
provider.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
package gotham
import (
"golang.org/x/oauth2"
"net/http"
)
type Provider interface {
Name() string
BeginUserAuth(http.ResponseWriter, *http.Request)
CompleteUserAuth(http.ResponseWriter, *http.Request) (token interface{}, err error) // returns x.oauth or x.oauth2 token depending on impl
FetchUserData(token interface{}) (*UserData, error) // consumes x.oauth or x.oauth2 token depending on impl
}
type provider struct {
*oauth2.Config
name string
}
func NewProvider(name string, config *oauth2.Config) Provider {
return &provider{
Config: config,
name: name,
}
}
func (p *provider) BeginUserAuth(w http.ResponseWriter, r *http.Request) {
state := NewState(w, r)
url := p.AuthCodeURL(state)
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
func (p *provider) CompleteUserAuth(w http.ResponseWriter, r *http.Request) (interface{}, error) {
defer ClearState(w) // remove saved auth_state when done
if err := ValidateState(r); err != nil {
return nil, err
}
return p.Exchange(oauth2.NoContext, GetAuthorizationCode(r))
}
func (p *provider) FetchUserData(t interface{}) (*UserData, error) {
return nil, ErrNotImplemented
}
func (p *provider) Name() string {
return p.name
}