mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
rename the sso to auth package
This commit is contained in:
53
auth/user.go
Normal file
53
auth/user.go
Normal file
@@ -0,0 +1,53 @@
|
||||
//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)
|
||||
}
|
||||
Reference in New Issue
Block a user