Skip to content

Commit 07ae457

Browse files
committed
Quick documentation pass.
1 parent 15e7ec0 commit 07ae457

File tree

2 files changed

+54
-17
lines changed

2 files changed

+54
-17
lines changed

README.md

+21-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
# Authboss Sample
2-
A sample implementer of authboss.
3-
4-
This is a simple blogging engine with a few basic features:
5-
6-
- Authentication provided by Authboss (all modules enabled, despite conflict between remember & expire)
7-
- Overridden (pretified) Authboss views.
8-
- CRUD for an in-memory storage of blogs.
9-
- Flash Messages
10-
- XSRF Protection
11-
12-
**Disclaimer:** This sample is NOT a seed project. Do not use it as one. It is used as an example of how to use the Authboss API.
13-
This means if you copy-paste code from this sample you are likely opening yourself up to various security holes, bad practice,
14-
and bad design. It's a demonstration of the surface API of Authboss and how the library can be used to make a functioning web
15-
project, to use this sample as anything else is malpractice.
1+
# Authboss Sample
2+
A sample implementation of authboss.
3+
4+
This is a simple blogging engine with a few basic features:
5+
6+
- Authentication provided by Authboss (all modules enabled with the exception of expire)
7+
- Some examples of overridden Authboss views.
8+
- CRUD for an in-memory storage of blogs.
9+
- Flash Messages
10+
- CSRF Protection (including authboss routes)
11+
- Support for API style JSON requests and responses (-api flag)
12+
- Various levels of debugging to see what's going wrong (-debug* flags)
13+
14+
# Disclaimer
15+
16+
This sample is **NOT** a seed project. Do not use it as one.
17+
It is used as an example of how to use the Authboss API. This means if
18+
you copy-paste code from this sample you are likely opening yourself
19+
up to various security holes, bad practice, and bad design.
20+
It's a demonstration of the surface API of Authboss and how the library
21+
can be used to make a functioning web project.

blog.go

+33-2
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,42 @@ func setupAuthboss() {
7171
ab.Config.Modules.LogoutMethod = "GET"
7272
}
7373

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.
7477
ab.Config.Storage.Server = database
7578
ab.Config.Storage.SessionState = NewSessionStorer()
7679
ab.Config.Storage.CookieState = NewCookieStorer()
7780

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.
7884
if *flagAPI {
7985
ab.Config.Core.ViewRenderer = defaults.JSONRenderer{}
8086
} else {
8187
ab.Config.Core.ViewRenderer = abrenderer.NewHTML("/auth", "ab_views")
8288
}
8389

90+
// We render mail with the authboss-renderer but we use a LogMailer
91+
// which simply sends the e-mail to stdout.
8492
ab.Config.Core.MailRenderer = abrenderer.NewEmail("/auth", "ab_views")
8593
ab.Config.Core.Mailer = defaults.LogMailer{}
8694

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)
8798
ab.Config.Modules.RegisterPreserveFields = []string{"email", "name"}
8899

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.
89103
defaults.SetCore(&ab.Config, *flagAPI, false)
90104

91105
// Here we initialize the bodyreader as something customized in order to accept a name
92106
// 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.
93110
emailRule := defaults.Rules{
94111
FieldName: "email", Required: true,
95112
MatchError: "Must be a valid e-mail address",
@@ -99,11 +116,15 @@ func setupAuthboss() {
99116
FieldName: "password", Required: true,
100117
MinLength: 4,
101118
}
119+
nameRule := defaults.Rules{
120+
FieldName: "name", Required: true,
121+
MinLength: 2,
122+
}
102123

103124
ab.Config.Core.BodyReader = defaults.HTTPBodyReader{
104125
ReadJSON: *flagAPI,
105126
Rulesets: map[string][]defaults.Rules{
106-
"register": {emailRule, passwordRule},
127+
"register": {emailRule, passwordRule, nameRule},
107128
"recover_end": {passwordRule},
108129
},
109130
Confirms: map[string][]string{
@@ -120,6 +141,8 @@ func setupAuthboss() {
120141
ClientSecret string `toml:"client_secret"`
121142
}{}
122143

144+
// Set up Google OAuth2 if we have credentials in the
145+
// file oauth2.toml for it.
123146
_, err := toml.DecodeFile("oauth2.toml", &oauthcreds)
124147
if err == nil && len(oauthcreds.ClientID) != 0 && len(oauthcreds.ClientSecret) != 0 {
125148
fmt.Println("oauth2.toml exists, configuring google oauth2")
@@ -140,6 +163,7 @@ func setupAuthboss() {
140163
fmt.Println("error loading oauth2.toml:", err)
141164
}
142165

166+
// Initialize authboss (instantiate modules etc.)
143167
if err := ab.Init(); err != nil {
144168
panic(err)
145169
}
@@ -148,6 +172,7 @@ func setupAuthboss() {
148172
func main() {
149173
flag.Parse()
150174

175+
// Load our application's templates
151176
if !*flagAPI {
152177
templates = tpl.Must(tpl.Load("views", "views/partials", "layout.html.tpl", funcs))
153178
}
@@ -176,13 +201,19 @@ func main() {
176201
cookieStore = securecookie.New(cookieStoreKey, nil)
177202
sessionStore = sessions.NewCookieStore(sessionStoreKey)
178203

179-
// Initialize ab.
204+
// Initialize authboss
180205
setupAuthboss()
181206

182207
// Set up our router
183208
schemaDec.IgnoreUnknownKeys(true)
184209

185210
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
186217
mux.Use(logger, nosurfing, ab.LoadClientStateMiddleware, remember.Middleware(ab), dataInjector)
187218

188219
// Authed routes

0 commit comments

Comments
 (0)