@@ -71,25 +71,42 @@ func setupAuthboss() {
71
71
ab .Config .Modules .LogoutMethod = "GET"
72
72
}
73
73
74
+ // Set up our server, session and cookie storage mechanisms.
75
+ // These are all from this package since the burden is on the
76
+ // implementer for these.
74
77
ab .Config .Storage .Server = database
75
78
ab .Config .Storage .SessionState = NewSessionStorer ()
76
79
ab .Config .Storage .CookieState = NewCookieStorer ()
77
80
81
+ // Another piece that we're responsible for: Rendering views.
82
+ // Though note that we're using the authboss-renderer package
83
+ // that makes the normal thing a bit easier.
78
84
if * flagAPI {
79
85
ab .Config .Core .ViewRenderer = defaults.JSONRenderer {}
80
86
} else {
81
87
ab .Config .Core .ViewRenderer = abrenderer .NewHTML ("/auth" , "ab_views" )
82
88
}
83
89
90
+ // We render mail with the authboss-renderer but we use a LogMailer
91
+ // which simply sends the e-mail to stdout.
84
92
ab .Config .Core .MailRenderer = abrenderer .NewEmail ("/auth" , "ab_views" )
85
93
ab .Config .Core .Mailer = defaults.LogMailer {}
86
94
95
+ // The preserve fields are things we don't want to
96
+ // lose when we're doing user registration (prevents having
97
+ // to type them again)
87
98
ab .Config .Modules .RegisterPreserveFields = []string {"email" , "name" }
88
99
100
+ // This instantiates and uses every default implementation
101
+ // in the Config.Core area that exist in the defaults package.
102
+ // Just a convenient helper if you don't want to do anything fancy.
89
103
defaults .SetCore (& ab .Config , * flagAPI , false )
90
104
91
105
// Here we initialize the bodyreader as something customized in order to accept a name
92
106
// parameter for our user as well as the standard e-mail and password.
107
+ //
108
+ // We also change the validation for these fields
109
+ // to be something less secure so that we can use test data easier.
93
110
emailRule := defaults.Rules {
94
111
FieldName : "email" , Required : true ,
95
112
MatchError : "Must be a valid e-mail address" ,
@@ -99,11 +116,15 @@ func setupAuthboss() {
99
116
FieldName : "password" , Required : true ,
100
117
MinLength : 4 ,
101
118
}
119
+ nameRule := defaults.Rules {
120
+ FieldName : "name" , Required : true ,
121
+ MinLength : 2 ,
122
+ }
102
123
103
124
ab .Config .Core .BodyReader = defaults.HTTPBodyReader {
104
125
ReadJSON : * flagAPI ,
105
126
Rulesets : map [string ][]defaults.Rules {
106
- "register" : {emailRule , passwordRule },
127
+ "register" : {emailRule , passwordRule , nameRule },
107
128
"recover_end" : {passwordRule },
108
129
},
109
130
Confirms : map [string ][]string {
@@ -120,6 +141,8 @@ func setupAuthboss() {
120
141
ClientSecret string `toml:"client_secret"`
121
142
}{}
122
143
144
+ // Set up Google OAuth2 if we have credentials in the
145
+ // file oauth2.toml for it.
123
146
_ , err := toml .DecodeFile ("oauth2.toml" , & oauthcreds )
124
147
if err == nil && len (oauthcreds .ClientID ) != 0 && len (oauthcreds .ClientSecret ) != 0 {
125
148
fmt .Println ("oauth2.toml exists, configuring google oauth2" )
@@ -140,6 +163,7 @@ func setupAuthboss() {
140
163
fmt .Println ("error loading oauth2.toml:" , err )
141
164
}
142
165
166
+ // Initialize authboss (instantiate modules etc.)
143
167
if err := ab .Init (); err != nil {
144
168
panic (err )
145
169
}
@@ -148,6 +172,7 @@ func setupAuthboss() {
148
172
func main () {
149
173
flag .Parse ()
150
174
175
+ // Load our application's templates
151
176
if ! * flagAPI {
152
177
templates = tpl .Must (tpl .Load ("views" , "views/partials" , "layout.html.tpl" , funcs ))
153
178
}
@@ -176,13 +201,19 @@ func main() {
176
201
cookieStore = securecookie .New (cookieStoreKey , nil )
177
202
sessionStore = sessions .NewCookieStore (sessionStoreKey )
178
203
179
- // Initialize ab.
204
+ // Initialize authboss
180
205
setupAuthboss ()
181
206
182
207
// Set up our router
183
208
schemaDec .IgnoreUnknownKeys (true )
184
209
185
210
mux := chi .NewRouter ()
211
+ // The middlewares we're using:
212
+ // - logger just does basic logging of requests and debug info
213
+ // - nosurfing is a more verbose wrapper around csrf handling
214
+ // - LoadClientStateMiddleware is required for session/cookie stuff
215
+ // - remember middleware logs users in if they have a remember token
216
+ // - dataInjector is for putting data into the request context we need for our template layout
186
217
mux .Use (logger , nosurfing , ab .LoadClientStateMiddleware , remember .Middleware (ab ), dataInjector )
187
218
188
219
// Authed routes
0 commit comments