3
0
corteza/pkg/auth/identity_context.go
2019-10-01 17:47:43 +02:00

47 lines
1015 B
Go

package auth
import (
"context"
)
type (
identityCtxKey struct{}
jwtCtxKey struct{}
)
func SetIdentityToContext(ctx context.Context, identity Identifiable) context.Context {
return context.WithValue(ctx, identityCtxKey{}, identity)
}
func GetIdentityFromContext(ctx context.Context) Identifiable {
if identity, ok := ctx.Value(identityCtxKey{}).(Identifiable); ok {
return identity
} else {
return NewIdentity(0)
}
}
func SetJwtToContext(ctx context.Context, jwt string) context.Context {
return context.WithValue(ctx, jwtCtxKey{}, jwt)
}
func GetJwtFromContext(ctx context.Context) string {
if jwt, ok := ctx.Value(jwtCtxKey{}).(string); ok {
return jwt
} else {
return ""
}
}
// SetSuperUserContext stores system user as identity
// and accompanying JWT for it to the context
func SetSuperUserContext(ctx context.Context) context.Context {
su := NewSuperUserIdentity()
ctx = SetIdentityToContext(ctx, su)
ctx = SetJwtToContext(ctx, DefaultJwtHandler.Encode(su))
return ctx
}