Skip to content

Commit

Permalink
cleanup errors, move setToken to be method, add cookie configs
Browse files Browse the repository at this point in the history
  • Loading branch information
nilslice committed Aug 23, 2017
1 parent d29ce10 commit 80f2c8f
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions access.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Config struct {
ExpireAfter time.Duration
ResponseWriter http.ResponseWriter
TokenStore reqHeaderOrHTTPCookie
SecureCookie bool
}

type reqHeaderOrHTTPCookie interface{}
Expand All @@ -46,16 +47,16 @@ func init() {
// to update the grant but will fail if unauthorized
func Grant(email, password string, cfg *Config) (*APIAccess, error) {
if email == "" {
return nil, fmt.Errorf("failed to create new APIAccess, %s", "email must not be empty")
return nil, fmt.Errorf("%s", "email must not be empty")
}

if password == "" {
return nil, fmt.Errorf("failed to create new APIAccess, %s", "password must not be empty")
return nil, fmt.Errorf("%s", "password must not be empty")
}

u, err := user.New(email, password)
if err != nil {
return nil, fmt.Errorf("failed to create new APIAccess, %v", err)
return nil, err
}

apiAccess := &APIAccess{
Expand All @@ -64,7 +65,7 @@ func Grant(email, password string, cfg *Config) (*APIAccess, error) {
Salt: u.Salt,
}

err = setToken(apiAccess, cfg)
err = apiAccess.setToken(cfg)
if err != nil {
return nil, err
}
Expand All @@ -78,7 +79,7 @@ func Grant(email, password string, cfg *Config) (*APIAccess, error) {
if b.Get([]byte(u.Email)) != nil {
err := updateGrant(email, password, cfg)
if err != nil {
return fmt.Errorf("failed to update APIAccess grant for %s", u.Email)
return fmt.Errorf("failed to update APIAccess grant for %s, %v", u.Email, err)
}
}

Expand Down Expand Up @@ -175,11 +176,11 @@ func getToken(req *http.Request, tokenStore reqHeaderOrHTTPCookie) (string, erro
return strings.TrimPrefix(bearer, "Bearer "), nil

default:
return "", fmt.Errorf("failed to get token, %s", "unrecognized token store")
return "", fmt.Errorf("%s", "unrecognized token store")
}
}

func setToken(a *APIAccess, cfg *Config) error {
func (a *APIAccess) setToken(cfg *Config) error {
exp := time.Now().Add(cfg.ExpireAfter)
claims := map[string]interface{}{
"exp": exp.Unix(),
Expand All @@ -199,14 +200,16 @@ func setToken(a *APIAccess, cfg *Config) error {

case http.Cookie:
http.SetCookie(cfg.ResponseWriter, &http.Cookie{
Name: apiAccessCookie,
Value: token,
Expires: exp,
Path: "/",
Name: apiAccessCookie,
Value: token,
Expires: exp,
Path: "/",
HttpOnly: true,
Secure: cfg.SecureCookie,
})

default:
return fmt.Errorf("failed to set token, %s", "unrecognized token store")
return fmt.Errorf("%s", "unrecognized token store")
}

return nil
Expand Down

0 comments on commit 80f2c8f

Please sign in to comment.