mirror of
https://github.com/kataras/iris.git
synced 2026-01-20 10:26:01 +00:00
New basic auth middleware and GetRaw on User (godocs missing)
This commit is contained in:
@@ -4726,7 +4726,7 @@ func (ctx *Context) UpsertCookie(cookie *http.Cookie, options ...CookieOption) b
|
||||
// you can change it or simple, use the SetCookie for more control.
|
||||
//
|
||||
// See `CookieExpires` and `AddCookieOptions` for more.
|
||||
var SetCookieKVExpiration = time.Duration(8760) * time.Hour
|
||||
var SetCookieKVExpiration = 8760 * time.Hour
|
||||
|
||||
// SetCookieKV adds a cookie, requires the name(string) and the value(string).
|
||||
//
|
||||
@@ -5343,7 +5343,15 @@ const userContextKey = "iris.user"
|
||||
// SetUser sets a value as a User for this request.
|
||||
// It's used by auth middlewares as a common
|
||||
// method to provide user information to the
|
||||
// next handlers in the chain
|
||||
// next handlers in the chain.
|
||||
//
|
||||
// The "i" input argument can be:
|
||||
// - A value which completes the User interface
|
||||
// - A map[string]interface{}.
|
||||
// - A value which does not complete the whole User interface
|
||||
// - A value which does not complete the User interface at all
|
||||
// (only its `User().GetRaw` method is available).
|
||||
//
|
||||
// Look the `User` method to retrieve it.
|
||||
func (ctx *Context) SetUser(i interface{}) error {
|
||||
if i == nil {
|
||||
@@ -5371,6 +5379,9 @@ func (ctx *Context) SetUser(i interface{}) error {
|
||||
}
|
||||
|
||||
// User returns the registered User of this request.
|
||||
// To get the original value (even if a value set by SetUser does not implement the User interface)
|
||||
// use its GetRaw method.
|
||||
// /
|
||||
// See `SetUser` too.
|
||||
func (ctx *Context) User() User {
|
||||
if v := ctx.values.Get(userContextKey); v != nil {
|
||||
|
||||
@@ -33,6 +33,8 @@ var ErrNotSupported = errors.New("not supported")
|
||||
// - UserMap (a wrapper by SetUser)
|
||||
// - UserPartial (a wrapper by SetUser)
|
||||
type User interface {
|
||||
// GetRaw should return the raw instance of the user, if supported.
|
||||
GetRaw() (interface{}, error)
|
||||
// GetAuthorization should return the authorization method,
|
||||
// e.g. Basic Authentication.
|
||||
GetAuthorization() (string, error)
|
||||
@@ -92,6 +94,11 @@ type SimpleUser struct {
|
||||
|
||||
var _ User = (*SimpleUser)(nil)
|
||||
|
||||
// GetRaw returns itself.
|
||||
func (u *SimpleUser) GetRaw() (interface{}, error) {
|
||||
return u, nil
|
||||
}
|
||||
|
||||
// GetAuthorization returns the authorization method,
|
||||
// e.g. Basic Authentication.
|
||||
func (u *SimpleUser) GetAuthorization() (string, error) {
|
||||
@@ -179,6 +186,11 @@ type UserMap Map
|
||||
|
||||
var _ User = UserMap{}
|
||||
|
||||
// GetRaw returns the underline map.
|
||||
func (u UserMap) GetRaw() (interface{}, error) {
|
||||
return Map(u), nil
|
||||
}
|
||||
|
||||
// GetAuthorization returns the authorization or Authorization value of the map.
|
||||
func (u UserMap) GetAuthorization() (string, error) {
|
||||
return u.str("authorization")
|
||||
@@ -292,11 +304,17 @@ type (
|
||||
GetID() string
|
||||
}
|
||||
|
||||
userGetUsername interface {
|
||||
// UserGetUsername interface which
|
||||
// requires a single method to complete
|
||||
// a User on Context.SetUser.
|
||||
UserGetUsername interface {
|
||||
GetUsername() string
|
||||
}
|
||||
|
||||
userGetPassword interface {
|
||||
// UserGetPassword interface which
|
||||
// requires a single method to complete
|
||||
// a User on Context.SetUser.
|
||||
UserGetPassword interface {
|
||||
GetPassword() string
|
||||
}
|
||||
|
||||
@@ -319,13 +337,14 @@ type (
|
||||
// UserPartial is a User.
|
||||
// It's a helper which wraps a struct value that
|
||||
// may or may not complete the whole User interface.
|
||||
// See Context.SetUser.
|
||||
UserPartial struct {
|
||||
Raw interface{}
|
||||
userGetAuthorization
|
||||
userGetAuthorizedAt
|
||||
userGetID
|
||||
userGetUsername
|
||||
userGetPassword
|
||||
UserGetUsername
|
||||
UserGetPassword
|
||||
userGetEmail
|
||||
userGetRoles
|
||||
userGetToken
|
||||
@@ -336,61 +355,64 @@ type (
|
||||
var _ User = (*UserPartial)(nil)
|
||||
|
||||
func newUserPartial(i interface{}) *UserPartial {
|
||||
containsAtLeastOneMethod := false
|
||||
if i == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
p := &UserPartial{Raw: i}
|
||||
|
||||
if u, ok := i.(userGetAuthorization); ok {
|
||||
p.userGetAuthorization = u
|
||||
containsAtLeastOneMethod = true
|
||||
}
|
||||
|
||||
if u, ok := i.(userGetAuthorizedAt); ok {
|
||||
p.userGetAuthorizedAt = u
|
||||
containsAtLeastOneMethod = true
|
||||
}
|
||||
|
||||
if u, ok := i.(userGetID); ok {
|
||||
p.userGetID = u
|
||||
containsAtLeastOneMethod = true
|
||||
}
|
||||
|
||||
if u, ok := i.(userGetUsername); ok {
|
||||
p.userGetUsername = u
|
||||
containsAtLeastOneMethod = true
|
||||
if u, ok := i.(UserGetUsername); ok {
|
||||
p.UserGetUsername = u
|
||||
}
|
||||
|
||||
if u, ok := i.(userGetPassword); ok {
|
||||
p.userGetPassword = u
|
||||
containsAtLeastOneMethod = true
|
||||
if u, ok := i.(UserGetPassword); ok {
|
||||
p.UserGetPassword = u
|
||||
}
|
||||
|
||||
if u, ok := i.(userGetEmail); ok {
|
||||
p.userGetEmail = u
|
||||
containsAtLeastOneMethod = true
|
||||
}
|
||||
|
||||
if u, ok := i.(userGetRoles); ok {
|
||||
p.userGetRoles = u
|
||||
containsAtLeastOneMethod = true
|
||||
}
|
||||
|
||||
if u, ok := i.(userGetToken); ok {
|
||||
p.userGetToken = u
|
||||
containsAtLeastOneMethod = true
|
||||
}
|
||||
|
||||
if u, ok := i.(userGetField); ok {
|
||||
p.userGetField = u
|
||||
containsAtLeastOneMethod = true
|
||||
}
|
||||
|
||||
if !containsAtLeastOneMethod {
|
||||
return nil
|
||||
}
|
||||
// if !containsAtLeastOneMethod {
|
||||
// return nil
|
||||
// }
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
// GetRaw returns the original raw instance of the user.
|
||||
func (u *UserPartial) GetRaw() (interface{}, error) {
|
||||
if u == nil {
|
||||
return nil, ErrNotSupported
|
||||
}
|
||||
|
||||
return u.Raw, nil
|
||||
}
|
||||
|
||||
// GetAuthorization should return the authorization method,
|
||||
// e.g. Basic Authentication.
|
||||
func (u *UserPartial) GetAuthorization() (string, error) {
|
||||
@@ -422,7 +444,7 @@ func (u *UserPartial) GetID() (string, error) {
|
||||
|
||||
// GetUsername should return the name of the User.
|
||||
func (u *UserPartial) GetUsername() (string, error) {
|
||||
if v := u.userGetUsername; v != nil {
|
||||
if v := u.UserGetUsername; v != nil {
|
||||
return v.GetUsername(), nil
|
||||
}
|
||||
|
||||
@@ -432,7 +454,7 @@ func (u *UserPartial) GetUsername() (string, error) {
|
||||
// GetPassword should return the encoded or raw password
|
||||
// (depends on the implementation) of the User.
|
||||
func (u *UserPartial) GetPassword() (string, error) {
|
||||
if v := u.userGetPassword; v != nil {
|
||||
if v := u.UserGetPassword; v != nil {
|
||||
return v.GetPassword(), nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user