mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 02:31:04 +01:00
54 lines
1.0 KiB
Go
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)
|
|
}
|