iris/auth/user.go
Gerasimos (Makis) Maropoulos 8652ee09f6
rename the sso to auth package
2022-04-02 17:30:55 +03:00

54 lines
1.0 KiB
Go

//go:build go1.18
package auth
import (
"github.com/kataras/iris/v12/context"
"github.com/kataras/jwt"
)
type (
StandardClaims = jwt.Claims
User = interface{} // any type.
)
const accessTokenContextKey = "iris.auth.context.access_token"
func GetAccessToken(ctx *context.Context) string {
return ctx.Values().GetString(accessTokenContextKey)
}
const standardClaimsContextKey = "iris.auth.context.standard_claims"
func GetStandardClaims(ctx *context.Context) StandardClaims {
if v := ctx.Values().Get(standardClaimsContextKey); v != nil {
if c, ok := v.(StandardClaims); ok {
return c
}
}
return StandardClaims{}
}
func (s *Auth[T]) GetStandardClaims(ctx *context.Context) StandardClaims {
return GetStandardClaims(ctx)
}
const userContextKey = "iris.auth.context.user"
func GetUser[T User](ctx *context.Context) T {
if v := ctx.Values().Get(userContextKey); v != nil {
if t, ok := v.(T); ok {
return t
}
}
var empty T
return empty
}
func (s *Auth[T]) GetUser(ctx *context.Context) T {
return GetUser[T](ctx)
}