1package easyauth 2 3import ( 4 "crypto/sha256" 5 "encoding/base64" 6 "fmt" 7 "html/template" 8 9 "github.com/gorilla/securecookie" 10) 11 12type Option func(*authManager) error 13 14func CookieSecret(s string) Option { 15 return func(a *authManager) error { 16 var dat []byte 17 var err error 18 //if valid b64, use that. best practice is a longish random base 64 string 19 if dat, err = base64.StdEncoding.DecodeString(s); err != nil { 20 dat = []byte(s) 21 } 22 if len(dat) < 8 { 23 return fmt.Errorf("Cookie secret is too small. Recommend 64 bytes in base 64 encoded string.") 24 } 25 var hashKey, blockKey []byte 26 if len(dat) == 64 { 27 hashKey, blockKey = dat[:32], dat[32:] 28 } else { 29 split := len(dat) / 2 30 h, e := sha256.Sum256(dat[split:]), sha256.Sum256(dat[:split]) 31 hashKey, blockKey = h[:], e[:] 32 } 33 a.cookie.sc = securecookie.New(hashKey, blockKey) 34 return nil 35 } 36} 37 38func CookieDuration(seconds int) Option { 39 return func(a *authManager) error { 40 a.cookie.duration = seconds 41 return nil 42 } 43} 44 45func LoginTemplate(t string) Option { 46 return func(a *authManager) error { 47 tmpl, err := template.New("login").Parse(t) 48 if err != nil { 49 return err 50 } 51 a.loginTemplate = tmpl 52 return nil 53 } 54} 55