forked from qor/auth
/
github.go
186 lines (153 loc) · 4.54 KB
/
github.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package github
import (
"errors"
"fmt"
"net/http"
"reflect"
"github.com/google/go-github/github"
"github.com/qor/auth"
"github.com/qor/auth/auth_identity"
"github.com/qor/auth/claims"
"github.com/qor/qor/utils"
"golang.org/x/oauth2"
)
var (
AuthorizeURL = "https://github.com/login/oauth/authorize"
TokenURL = "https://github.com/login/oauth/access_token"
)
// GithubProvider provide login with github method
type GithubProvider struct {
*Config
}
// Config github Config
type Config struct {
ClientID string
ClientSecret string
AuthorizeURL string
TokenURL string
RedirectURL string
Scopes []string
AuthorizeHandler func(*auth.Context) (*claims.Claims, error)
}
func New(config *Config) *GithubProvider {
if config == nil {
config = &Config{}
}
provider := &GithubProvider{Config: config}
if config.ClientID == "" {
panic(errors.New("Github's ClientID can't be blank"))
}
if config.ClientSecret == "" {
panic(errors.New("Github's ClientSecret can't be blank"))
}
if config.AuthorizeURL == "" {
config.AuthorizeURL = AuthorizeURL
}
if config.TokenURL == "" {
config.TokenURL = TokenURL
}
if config.AuthorizeHandler == nil {
config.AuthorizeHandler = func(context *auth.Context) (*claims.Claims, error) {
var (
schema auth.Schema
authInfo auth_identity.Basic
authIdentity = reflect.New(utils.ModelType(context.Auth.Config.AuthIdentityModel)).Interface()
req = context.Request
tx = context.Auth.GetDB(req)
)
state := req.URL.Query().Get("state")
claims, err := context.Auth.SessionStorer.ValidateClaims(state)
if err != nil || claims.Valid() != nil || claims.Subject != "state" {
return nil, auth.ErrUnauthorized
}
if err == nil {
oauthCfg := provider.OAuthConfig(context)
tkn, err := oauthCfg.Exchange(oauth2.NoContext, req.URL.Query().Get("code"))
if err != nil {
return nil, err
}
client := github.NewClient(oauthCfg.Client(oauth2.NoContext, tkn))
user, _, err := client.Users.Get(oauth2.NoContext, "")
if err != nil {
return nil, err
}
authInfo.Provider = provider.GetName()
authInfo.UID = fmt.Sprint(*user.ID)
if !tx.Model(authIdentity).Where(authInfo).Scan(&authInfo).RecordNotFound() {
return authInfo.ToClaims(), nil
}
{
schema.Provider = provider.GetName()
schema.UID = fmt.Sprint(*user.ID)
schema.Name = user.GetName()
schema.Email = user.GetEmail()
schema.Image = user.GetAvatarURL()
schema.RawInfo = user
}
if _, userID, err := context.Auth.UserStorer.Save(&schema, context); err == nil {
if userID != "" {
authInfo.UserID = userID
}
} else {
return nil, err
}
if err = tx.Where(authInfo).FirstOrCreate(authIdentity).Error; err == nil {
return authInfo.ToClaims(), nil
}
return nil, err
}
return nil, err
}
}
return provider
}
// GetName return provider name
func (GithubProvider) GetName() string {
return "github"
}
// ConfigAuth config auth
func (provider GithubProvider) ConfigAuth(*auth.Auth) {
}
// OAuthConfig return oauth config based on configuration
func (provider GithubProvider) OAuthConfig(context *auth.Context) *oauth2.Config {
var (
config = provider.Config
req = context.Request
scheme = req.URL.Scheme
)
if scheme == "" {
scheme = "http://"
}
return &oauth2.Config{
ClientID: config.ClientID,
ClientSecret: config.ClientSecret,
Endpoint: oauth2.Endpoint{
AuthURL: config.AuthorizeURL,
TokenURL: config.TokenURL,
},
RedirectURL: scheme + req.Host + context.Auth.AuthURL("github/callback"),
Scopes: config.Scopes,
}
}
// Login implemented login with github provider
func (provider GithubProvider) Login(context *auth.Context) {
claims := claims.Claims{}
claims.Subject = "state"
signedToken := context.Auth.SessionStorer.SignedToken(&claims)
url := provider.OAuthConfig(context).AuthCodeURL(signedToken)
http.Redirect(context.Writer, context.Request, url, http.StatusFound)
}
// Logout implemented logout with github provider
func (GithubProvider) Logout(context *auth.Context) {
}
// Register implemented register with github provider
func (provider GithubProvider) Register(context *auth.Context) {
provider.Login(context)
}
// Callback implement Callback with github provider
func (provider GithubProvider) Callback(context *auth.Context) {
context.Auth.LoginHandler(context, provider.AuthorizeHandler)
}
// ServeHTTP implement ServeHTTP with github provider
func (GithubProvider) ServeHTTP(*auth.Context) {
}