You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
789 B
Go
43 lines
789 B
Go
2 years ago
|
package auth
|
||
|
|
||
|
type User interface {
|
||
|
GetUsername() string
|
||
|
GetName() string
|
||
|
GetSurname() string
|
||
|
GetFullName() string
|
||
|
}
|
||
|
|
||
|
type Session interface {
|
||
|
GetUsername() string
|
||
|
GetToken() string
|
||
|
}
|
||
|
|
||
|
type AuthenticatorService interface {
|
||
|
GetUser(username string) (User, error)
|
||
|
GetUsers() ([]User, error)
|
||
|
GetSession(token string) (Session, error)
|
||
|
Login(username, password string) (Session, error)
|
||
|
}
|
||
|
|
||
|
func UserForSession(as AuthenticatorService, token string) (User, error) {
|
||
|
session, err := as.GetSession(token)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
user, err := as.GetUser(session.GetUsername())
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return user, nil
|
||
|
}
|
||
|
|
||
|
func New(host string) AuthenticatorService {
|
||
|
if host == ":memory:" {
|
||
|
return exampleMemoryUsers
|
||
|
}
|
||
|
|
||
|
return &LDAPAuthService{host}
|
||
|
}
|