This repository has been archived by the owner on Mar 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathauth.go
85 lines (76 loc) · 1.93 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
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
package photoshare
import (
"github.com/juju/errgo"
"github.com/stretchr/gomniauth"
"github.com/stretchr/gomniauth/common"
"github.com/stretchr/gomniauth/providers/google"
"github.com/stretchr/objx"
"github.com/stretchr/signature"
"net/http"
)
type authInfo struct {
name, email string
}
type authenticator interface {
getRedirectURL(*http.Request, string) (string, error)
getUserInfo(*http.Request, string) (*authInfo, error)
}
func newAuthenticator(cfg *config) authenticator {
gomniauth.SetSecurityKey(signature.RandomKey(64))
a := &defaultAuthenticator{cfg}
return a
}
type defaultAuthenticator struct {
cfg *config
}
func (a *defaultAuthenticator) getAuthProvider(r *http.Request, providerName string) (common.Provider, error) {
gomniauth.WithProviders(
google.New(a.cfg.GoogleClientID,
a.cfg.GoogleSecret,
getBaseURL(r)+"/api/auth/oauth2/google/callback/",
),
)
provider, err := gomniauth.Provider(providerName)
if err != nil {
return provider, errgo.Mask(err)
}
return provider, nil
}
func (a *defaultAuthenticator) getRedirectURL(r *http.Request, providerName string) (string, error) {
provider, err := a.getAuthProvider(r, providerName)
if err != nil {
return "", errgo.Mask(err)
}
state := gomniauth.NewState("after", "success")
url, err := provider.GetBeginAuthURL(state, nil)
if err != nil {
return url, errgo.Mask(err)
}
return url, nil
}
func (a *defaultAuthenticator) getUserInfo(r *http.Request, providerName string) (*authInfo, error) {
provider, err := a.getAuthProvider(r, providerName)
if err != nil {
return nil, errgo.Mask(err)
}
m := make(objx.Map)
if r.Form == nil {
r.ParseForm()
}
for k, v := range r.Form {
m.Set(k, v)
}
creds, err := provider.CompleteAuth(m)
if err != nil {
return nil, errgo.Mask(err)
}
user, err := provider.GetUser(creds)
if err != nil {
return nil, errgo.Mask(err)
}
info := &authInfo{
name: user.Name(),
email: user.Email(),
}
return info, nil
}