2015-01-11 08:52:39 +02:00
|
|
|
// Package remember implements persistent logins through (typically) cookie session
|
|
|
|
// storages. The SessionStorer implementation must be fully secure either over https
|
|
|
|
// or using signed cookies or it is easily exploitable.
|
|
|
|
package remember
|
|
|
|
|
|
|
|
import (
|
2015-01-13 00:02:07 +02:00
|
|
|
"bytes"
|
2015-01-11 08:52:39 +02:00
|
|
|
"crypto/md5"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
2015-01-13 00:02:07 +02:00
|
|
|
"fmt"
|
2015-01-11 08:52:39 +02:00
|
|
|
|
|
|
|
"gopkg.in/authboss.v0"
|
|
|
|
)
|
|
|
|
|
2015-01-13 00:02:07 +02:00
|
|
|
const (
|
|
|
|
// ValueKey is used for cookies and form input names.
|
|
|
|
ValueKey = "rm"
|
|
|
|
)
|
|
|
|
|
2015-01-11 08:52:39 +02:00
|
|
|
const nRandBytes = 32
|
|
|
|
|
|
|
|
// R is the singleton instance of the remember module which will have been
|
|
|
|
// configured and ready to use after authboss.Init()
|
|
|
|
var R *Remember
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
R = &Remember{}
|
|
|
|
authboss.RegisterModule("remember", R)
|
|
|
|
}
|
|
|
|
|
2015-02-16 06:07:36 +02:00
|
|
|
type Remember struct{}
|
2015-01-11 08:52:39 +02:00
|
|
|
|
2015-02-16 06:07:36 +02:00
|
|
|
func (r *Remember) Initialize() error {
|
|
|
|
if authboss.Cfg.Storer == nil {
|
2015-01-13 00:02:07 +02:00
|
|
|
return errors.New("remember: Need a TokenStorer.")
|
|
|
|
}
|
|
|
|
|
2015-02-16 06:07:36 +02:00
|
|
|
if _, ok := authboss.Cfg.Storer.(authboss.TokenStorer); !ok {
|
2015-01-13 00:02:07 +02:00
|
|
|
return errors.New("remember: TokenStorer required for remember me functionality.")
|
2015-01-11 08:52:39 +02:00
|
|
|
}
|
|
|
|
|
2015-02-16 06:07:36 +02:00
|
|
|
authboss.Cfg.Callbacks.After(authboss.EventAuth, r.AfterAuth)
|
2015-01-13 00:02:07 +02:00
|
|
|
|
2015-01-11 08:52:39 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Remember) Routes() authboss.RouteTable {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Remember) Storage() authboss.StorageOptions {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-13 00:02:07 +02:00
|
|
|
// AfterAuth is called after authentication is successful.
|
|
|
|
func (r *Remember) AfterAuth(ctx *authboss.Context) {
|
2015-01-16 01:10:47 +02:00
|
|
|
if val, ok := ctx.FirstPostFormValue(ValueKey); !ok || val != "true" {
|
2015-01-13 00:02:07 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-15 12:56:13 +02:00
|
|
|
if ctx.User == nil {
|
2015-02-16 06:07:36 +02:00
|
|
|
fmt.Fprintf(authboss.Cfg.LogWriter, "remember: AfterAuth no user loaded")
|
2015-01-15 23:24:12 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
keyIntf, ok := ctx.User["username"]
|
|
|
|
if !ok {
|
2015-02-16 06:07:36 +02:00
|
|
|
fmt.Fprintf(authboss.Cfg.LogWriter, "remember: username not present")
|
2015-01-15 23:24:12 +02:00
|
|
|
return
|
|
|
|
}
|
2015-01-16 01:10:47 +02:00
|
|
|
|
2015-01-15 23:24:12 +02:00
|
|
|
key, ok := keyIntf.(string)
|
|
|
|
if !ok {
|
2015-02-16 06:07:36 +02:00
|
|
|
fmt.Fprintf(authboss.Cfg.LogWriter, "remember: username not a string")
|
2015-01-15 23:24:12 +02:00
|
|
|
return
|
2015-01-15 05:18:45 +02:00
|
|
|
}
|
2015-01-13 00:02:07 +02:00
|
|
|
|
|
|
|
if _, err := r.New(ctx.CookieStorer, key); err != nil {
|
2015-02-16 06:07:36 +02:00
|
|
|
fmt.Fprintf(authboss.Cfg.LogWriter, "remember: Failed to create remember token: %v", err)
|
2015-01-13 00:02:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-11 08:52:39 +02:00
|
|
|
// New generates a new remember token and stores it in the configured TokenStorer.
|
|
|
|
// The return value is a token that should only be given to a user if the delivery
|
|
|
|
// method is secure which means at least signed if not encrypted.
|
2015-01-13 00:02:07 +02:00
|
|
|
func (r *Remember) New(cstorer authboss.ClientStorer, storageKey string) (string, error) {
|
|
|
|
token := make([]byte, nRandBytes+len(storageKey)+1)
|
|
|
|
copy(token, []byte(storageKey))
|
|
|
|
token[len(storageKey)] = ';'
|
2015-01-11 08:52:39 +02:00
|
|
|
|
2015-01-13 00:02:07 +02:00
|
|
|
if _, err := rand.Read(token[len(storageKey)+1:]); err != nil {
|
|
|
|
return "", err
|
2015-01-11 08:52:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sum := md5.Sum(token)
|
|
|
|
finalToken := base64.URLEncoding.EncodeToString(token)
|
|
|
|
storageToken := base64.StdEncoding.EncodeToString(sum[:])
|
|
|
|
|
2015-01-13 00:02:07 +02:00
|
|
|
// Save the token in the DB
|
2015-02-16 06:07:36 +02:00
|
|
|
if err := authboss.Cfg.Storer.(authboss.TokenStorer).AddToken(storageKey, storageToken); err != nil {
|
2015-01-11 08:52:39 +02:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-01-13 00:02:07 +02:00
|
|
|
// Write the finalToken to the cookie
|
|
|
|
cstorer.Put(ValueKey, finalToken)
|
|
|
|
|
2015-01-11 08:52:39 +02:00
|
|
|
return finalToken, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Auth takes a token that was given to a user and checks to see if something
|
|
|
|
// is matching in the database. If something is found the old token is deleted
|
|
|
|
// and a new one should be generated. The return value is the key of the
|
|
|
|
// record who owned this token.
|
2015-01-13 00:02:07 +02:00
|
|
|
func (r *Remember) Auth(
|
|
|
|
cstorer authboss.ClientStorer,
|
|
|
|
sstorer authboss.ClientStorer,
|
|
|
|
finalToken string) (string, error) {
|
|
|
|
|
2015-01-11 08:52:39 +02:00
|
|
|
token, err := base64.URLEncoding.DecodeString(finalToken)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-01-13 00:02:07 +02:00
|
|
|
index := bytes.IndexByte(token, ';')
|
|
|
|
if index < 0 {
|
|
|
|
return "", errors.New("remember: Invalid remember me token.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the key.
|
|
|
|
givenKey := token[:index]
|
|
|
|
|
|
|
|
// Verify the tokens match.
|
2015-01-11 08:52:39 +02:00
|
|
|
sum := md5.Sum(token)
|
2015-01-13 00:02:07 +02:00
|
|
|
|
2015-02-16 06:07:36 +02:00
|
|
|
key, err := authboss.Cfg.Storer.(authboss.TokenStorer).UseToken(string(givenKey), base64.StdEncoding.EncodeToString(sum[:]))
|
2015-01-24 01:56:24 +02:00
|
|
|
if err == authboss.ErrTokenNotFound {
|
2015-01-11 08:52:39 +02:00
|
|
|
return "", nil
|
|
|
|
} else if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-01-13 00:02:07 +02:00
|
|
|
// Ensure a half-auth.
|
2015-01-16 02:04:33 +02:00
|
|
|
sstorer.Put(authboss.HalfAuthKey, "true")
|
2015-01-13 00:02:07 +02:00
|
|
|
// Log the user in.
|
|
|
|
sstorer.Put(authboss.SessionKey, key)
|
|
|
|
|
2015-01-11 08:52:39 +02:00
|
|
|
return key, nil
|
|
|
|
}
|