/
app.go
268 lines (248 loc) · 8.51 KB
/
app.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package users
import (
"fmt"
"reflect"
"gnd.la/app"
"gnd.la/app/reusableapp"
"gnd.la/social/facebook"
"gnd.la/social/github"
"gnd.la/social/google"
"gnd.la/social/twitter"
"gnd.la/template"
"gnd.la/template/assets"
_ "gnd.la/template/assets/sass" // import the sass compiler for the scss assets
"gnd.la/util/generic"
"gnd.la/util/structs"
)
type appData struct {
opts Options
userType reflect.Type
socialAccountTypes []*socialAccountType
}
type App struct {
reusableapp.App
}
func (a *App) Attach(parent *app.App) {
parent.SetUserFunc(userFunc)
a.App.Attach(parent)
}
func (d *appData) setUserType(user interface{}) {
var typ reflect.Type
if tt, ok := user.(reflect.Type); ok {
typ = tt
} else {
typ = reflect.TypeOf(user)
}
if typ != nil {
for typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
}
if typ == nil {
panic(fmt.Errorf("User type is not set - configure it with users.SetType(&MyUserType{})"))
}
s, err := structs.New(typ, nil, nil)
if err != nil {
panic(err)
}
if !s.Embeds(innerType) {
panic(fmt.Errorf("invalid User type %s: must embed %s e.g type %s struct {\t\t%s\n\t...\n}", typ, innerType, typ.Name(), innerType))
}
for _, v := range d.enabledSocialAccountTypes() {
if !s.Has(v.Name.String(), v.Type) {
panic(&missingFieldError{typ, v.Name.String(), v.Type})
}
}
d.userType = typ
}
func (d *appData) allowDirectSignIn() bool {
return !d.opts.DisableDirectSignIn
}
func (d *appData) allowRegistration() bool {
return !d.opts.disableRegistration()
}
func (d *appData) hasEnabledSocialSignin() bool {
return len(d.enabledSocialAccountTypes()) > 0
}
func (d *appData) enabledSocialAccountTypes() []*socialAccountType {
if d.socialAccountTypes == nil {
st := make([]*socialAccountType, 0)
if d.opts.FacebookApp != nil {
st = append(st, socialAccountTypes[SocialAccountTypeFacebook])
}
if d.opts.TwitterApp != nil {
st = append(st, socialAccountTypes[SocialAccountTypeTwitter])
}
if d.opts.GoogleApp != nil {
st = append(st, socialAccountTypes[SocialAccountTypeGoogle])
}
if d.opts.GithubApp != nil {
st = append(st, socialAccountTypes[SocialAccountTypeGithub])
}
positions := make(map[SocialAccountType]int)
for k := range socialAccountTypes {
positions[k] = -1
}
for ii, k := range d.opts.SocialAccountsOrder {
positions[k] = ii
}
generic.SortFunc(st, func(s1, s2 *socialAccountType) bool {
p1, p2 := positions[s1.Name], positions[s2.Name]
if p1 == -1 {
return false
}
if p2 == -1 {
return true
}
return p1 < p2
})
d.socialAccountTypes = st
}
return d.socialAccountTypes
}
// Options specifies the options for a *UsersApp. See each field
// for further explanations.
type Options struct {
// The name of the site which will appear in user visible messages
// e.g. "Sign In to SiteName".
SiteName string
// The user type, represented by a instance of the type.
// e.g.
//
// users{Options{UserType: MyUser{}}}
UserType interface{}
FacebookApp *facebook.App
FacebookPermissions []string
GoogleApp *google.App
GoogleScopes []string
TwitterApp *twitter.App
GithubApp *github.App
GithubScopes []string
// SocialAccountsOrder can be used to set the order in which
// the enabled social accounts appear on the screen, from top
// to bottom. If empty, it defaults to:
//
// []string{SocialTypeFacebook, SocialTypeTwitter, SocialTypeGoogle, SocialTypeGithub}
SocialAccountsOrder []SocialAccountType
// DisableDirectSignIn can be used to disable non-social sign ins. If it's set to
// true, users will only be able to sign in using the social sign in options.
// Note that setting this variable to true also disables user registration, regardless
// of the value of the DisableRegistration field.
DisableDirectSignIn bool
// DisableRegistration can be used to disable user registration. Only existing users
// and social accounts will be able to log in.
DisableRegistration bool
}
func (o *Options) googleScopes() []string {
if o.GoogleScopes != nil {
return o.GoogleScopes
}
// XXX: Keep this in sync with Options.GoogleScopes
return []string{google.PlusScope, google.EmailScope}
}
func (o *Options) githubScopes() []string {
if o.GithubScopes != nil {
return o.GithubScopes
}
// XXX: Keep this in sync with comment on Options.GithubScopes
return []string{github.ScopeEmail}
}
func (o *Options) socialAccountsOrder() []SocialAccountType {
if o.SocialAccountsOrder != nil {
return o.SocialAccountsOrder
}
// XXX: Keep this in sync with comment on Options.SocialAccountsOrder
return []SocialAccountType{
SocialAccountTypeFacebook,
SocialAccountTypeTwitter,
SocialAccountTypeGoogle,
SocialAccountTypeGithub,
}
}
func (o *Options) disableRegistration() bool {
return o.DisableRegistration || o.DisableDirectSignIn
}
type key int
const (
appDataKey key = iota
)
func New(opts Options) *App {
d := &appData{
opts: opts,
}
d.setUserType(opts.UserType)
a := &App{App: *reusableapp.New(reusableapp.Options{
Name: "Users",
Data: d,
DataKey: appDataKey,
AssetsData: assetsData,
TemplatesData: tmplData,
})}
a.Prefix = "/users/"
a.AddTemplateVars(map[string]interface{}{
"SiteName": opts.SiteName,
"FacebookApp": opts.FacebookApp,
"GoogleApp": opts.GoogleApp,
"GoogleScopes": opts.GoogleScopes,
"TwitterApp": opts.TwitterApp,
"GithubApp": opts.GithubApp,
"JSSignIn": JSSignInHandlerName,
"JSSignInFacebook": JSSignInFacebookHandlerName,
"JSSignInGoogle": JSSignInGoogleHandlerName,
"JSSignUp": JSSignUpHandlerName,
"Forgot": ForgotHandlerName,
"Reset": ResetHandlerName,
"SignIn": func() string { return SignInHandlerName },
"SignInFacebook": SignInFacebookHandlerName,
"SignInGoogle": SignInGoogleHandlerName,
"SignInTwitter": SignInTwitterHandlerName,
"SignInGithub": SignInGithubHandlerName,
"SignUp": SignUpHandlerName,
"SignOut": SignOutHandlerName,
"FacebookChannel": FacebookChannelHandlerName,
"Current": Current,
"DisableDirectSignIn": opts.DisableDirectSignIn,
"DisableRegistration": opts.disableRegistration(),
"SocialAccountTypes": d.enabledSocialAccountTypes(),
})
a.Handle("^/sign-in/$", SignInHandler, app.NamedHandler(SignInHandlerName))
if opts.FacebookApp != nil {
signInFacebookHandler := oauth2SignInHandler(signInFacebookTokenHandler,
opts.FacebookApp.Client, opts.FacebookPermissions)
a.Handle("^/sign-in/facebook/$", signInFacebookHandler, app.NamedHandler(SignInFacebookHandlerName))
a.Handle("^/js/sign-in/facebook/$", JSSignInFacebookHandler, app.NamedHandler(JSSignInFacebookHandlerName))
a.Handle("^/fb-channel/$", FacebookChannelHandler, app.NamedHandler(FacebookChannelHandlerName))
}
if opts.GoogleApp != nil {
signInGoogleHandler := oauth2SignInHandler(signInGoogleTokenHandler,
opts.GoogleApp.Client, opts.googleScopes())
a.Handle("^/sign-in/google/$", signInGoogleHandler, app.NamedHandler(SignInGoogleHandlerName))
a.Handle("^/js/sign-in/google/$", JSSignInGoogleHandler, app.NamedHandler(JSSignInGoogleHandlerName))
}
if opts.TwitterApp != nil {
signInTwitterHandler := twitter.AuthHandler(opts.TwitterApp, signInTwitterUserHandler)
a.Handle("^/sign-in/twitter/$", signInTwitterHandler, app.NamedHandler(SignInTwitterHandlerName))
}
if opts.GithubApp != nil {
signInGithubHandler := oauth2SignInHandler(signInGithubTokenHandler,
opts.GithubApp.Client, opts.githubScopes())
a.Handle("^/sign-in/github/$", signInGithubHandler, app.NamedHandler(SignInGithubHandlerName))
}
a.Handle("^/sign-up/$", SignUpHandler, app.NamedHandler(SignUpHandlerName))
a.Handle("^/sign-out/$", SignOutHandler, app.NamedHandler(SignOutHandlerName))
a.Handle("^/forgot/$", ForgotHandler, app.NamedHandler(ForgotHandlerName))
a.Handle("^/reset/$", ResetHandler, app.NamedHandler(ResetHandlerName))
a.Handle("^/js/sign-in/$", JSSignInHandler, app.NamedHandler(JSSignInHandlerName))
a.Handle("^/js/sign-up/$", JSSignUpHandler, app.NamedHandler(JSSignUpHandlerName))
template.AddFuncs([]*template.Func{
{Name: "user_image", Fn: Image, Traits: template.FuncTraitContext},
{Name: "__users_get_social", Fn: getSocial},
})
a.MustLoadTemplatePlugin("users-plugin.html", assets.Bottom)
a.MustLoadTemplatePlugin("social-button.html", assets.None)
return a
}
func data(ctx *app.Context) *appData {
d, _ := reusableapp.AppDataWithKey(ctx.App(), appDataKey).(*appData)
return d
}