fleet/server/contexts/token/token.go
Victor Vrantchan 372dac8304 Create context packages (#228)
add token context package
add viewer context package
add host context package
update authenticated middleware to set viewer context or return error
re-organize API handler
2016-09-26 13:14:39 -04:00

38 lines
922 B
Go

// Package token enables setting and reading
// authentication token contexts
package token
import (
"context"
"net/http"
"strings"
)
type key int
const tokenKey key = 0
// FromHTTPRequest extracts an Authorization
// from an HTTP header if present.
func FromHTTPRequest(r *http.Request) string {
headers := r.Header.Get("Authorization")
headerParts := strings.Split(headers, " ")
if len(headerParts) != 2 || strings.ToUpper(headerParts[0]) != "BEARER" {
return ""
}
return headerParts[1]
}
// NewContext returns a new context carrying the Authorization Bearer token.
func NewContext(ctx context.Context, token string) context.Context {
if token == "" {
return ctx
}
return context.WithValue(ctx, tokenKey, token)
}
// FromContext extracts the Authorization Bearer token if present.
func FromContext(ctx context.Context) (string, bool) {
token, ok := ctx.Value(tokenKey).(string)
return token, ok
}