Documentation ¶
Index ¶
- Constants
- Variables
- func MigrateAll(db *gorm.DB) error
- func RegisterRoutes(router *mux.Router, service *Service)
- type AccessToken
- type AccessTokenResponse
- type AuthorizationCode
- type Client
- type IntrospectResponse
- type RefreshToken
- type Scope
- type Service
- func (s *Service) AuthClient(clientID, secret string) (*Client, error)
- func (s *Service) AuthUser(username, password string) (*User, error)
- func (s *Service) Authenticate(token string) (*AccessToken, error)
- func (s *Service) ClientExists(clientID string) bool
- func (s *Service) CreateClient(clientID, secret, redirectURI string) (*Client, error)
- func (s *Service) CreateClientTx(tx *gorm.DB, clientID, secret, redirectURI string) (*Client, error)
- func (s *Service) CreateUser(username, password string) (*User, error)
- func (s *Service) CreateUserTx(tx *gorm.DB, username, password string) (*User, error)
- func (s *Service) FindClientByClientID(clientID string) (*Client, error)
- func (s *Service) FindUserByUsername(username string) (*User, error)
- func (s *Service) GetOrCreateRefreshToken(client *Client, user *User, expiresIn int, scope string) (*RefreshToken, error)
- func (s *Service) GetScope(requestedScope string) (string, error)
- func (s *Service) GetValidRefreshToken(token string, client *Client) (*RefreshToken, error)
- func (s *Service) GrantAccessToken(client *Client, user *User, expiresIn int, scope string) (*AccessToken, error)
- func (s *Service) GrantAuthorizationCode(client *Client, user *User, expiresIn int, redirectURI, scope string) (*AuthorizationCode, error)
- func (s *Service) IntrospectResponseAccessToken(at *AccessToken) *IntrospectResponse
- func (s *Service) IntrospectResponseRefreshToken(rt *RefreshToken) *IntrospectResponse
- func (s *Service) Login(client *Client, user *User, scope string) (*AccessToken, *RefreshToken, error)
- func (s *Service) SetPassword(user *User, password string) error
- func (s *Service) UserExists(username string) bool
- type ServiceInterface
- type ServiceMock
- func (_m *ServiceMock) AuthClient(clientID string, secret string) (*Client, error)
- func (_m *ServiceMock) AuthUser(username string, thePassword string) (*User, error)
- func (_m *ServiceMock) Authenticate(token string) (*AccessToken, error)
- func (_m *ServiceMock) ClientExists(clientID string) bool
- func (_m *ServiceMock) CreateClient(clientID string, secret string, redirectURI string) (*Client, error)
- func (_m *ServiceMock) CreateClientTx(tx *gorm.DB, clientID string, secret string, redirectURI string) (*Client, error)
- func (_m *ServiceMock) CreateUser(username string, password string) (*User, error)
- func (_m *ServiceMock) CreateUserTx(tx *gorm.DB, username string, password string) (*User, error)
- func (_m *ServiceMock) FindClientByClientID(clientID string) (*Client, error)
- func (_m *ServiceMock) FindUserByUsername(username string) (*User, error)
- func (_m *ServiceMock) GetOrCreateRefreshToken(client *Client, user *User, expiresIn int, scope string) (*RefreshToken, error)
- func (_m *ServiceMock) GetScope(requestedScope string) (string, error)
- func (_m *ServiceMock) GetValidRefreshToken(token string, client *Client) (*RefreshToken, error)
- func (_m *ServiceMock) GrantAccessToken(client *Client, user *User, expiresIn int, scope string) (*AccessToken, error)
- func (_m *ServiceMock) GrantAuthorizationCode(client *Client, user *User, expiresIn int, redirectURI string, scope string) (*AuthorizationCode, error)
- func (_m *ServiceMock) Login(client *Client, user *User, scope string) (*AccessToken, *RefreshToken, error)
- func (_m *ServiceMock) SetPassword(user *User, password string) error
- func (_m *ServiceMock) UserExists(username string) bool
- type User
Constants ¶
const TokenType = "Bearer"
TokenType is default type of generated tokens.
Variables ¶
var ( // ErrAccessTokenNotFound ... ErrAccessTokenNotFound = errors.New("Access token not found") // ErrAccessTokenExpired ... ErrAccessTokenExpired = errors.New("Access token expired") )
var ( // ErrAuthorizationCodeNotFound ... ErrAuthorizationCodeNotFound = errors.New("Authorization code not found") // ErrAuthorizationCodeExpired ... ErrAuthorizationCodeExpired = errors.New("Authorization code expired") )
var ( // ErrClientNotFound ... ErrClientNotFound = errors.New("Client not found") // ErrInvalidClientSecret ... ErrInvalidClientSecret = errors.New("Invalid client secret") )
var ( // ErrInvalidGrantType ... ErrInvalidGrantType = errors.New("Invalid grant type") // ErrClientAuthenticationRequired ... ErrClientAuthenticationRequired = errors.New("Client authentication required") )
var ( // ErrTokenMissing ... ErrTokenMissing = errors.New("Token missing") // ErrTokenHintInvalid ... ErrTokenHintInvalid = errors.New("Invalid token hint") )
var ( // ErrRefreshTokenNotFound ... ErrRefreshTokenNotFound = errors.New("Refresh token not found") // ErrRefreshTokenExpired ... ErrRefreshTokenExpired = errors.New("Refresh token expired") )
var ( // ErrUserNotFound ... ErrUserNotFound = errors.New("User not found") // ErrInvalidUserPassword ... ErrInvalidUserPassword = errors.New("Invalid user password") // ErrCannotSetEmptyUserPassword ... ErrCannotSetEmptyUserPassword = errors.New("Cannot set empty user password") // ErrUserPasswordNotSet ... ErrUserPasswordNotSet = errors.New("User password not set") )
var ( // ErrInvalidRedirectURI ... ErrInvalidRedirectURI = errors.New("Invalid redirect URI") )
var ( // ErrInvalidScope ... ErrInvalidScope = errors.New("Invalid scope") )
var ( // ErrRequestedScopeCannotBeGreater ... ErrRequestedScopeCannotBeGreater = errors.New("Requested scope cannot be greater") )
var ( // ErrUserAuthenticationRequired ... ErrUserAuthenticationRequired = errors.New("User authentication required") )
Functions ¶
func RegisterRoutes ¶
RegisterRoutes registers route handlers for the oauth service
Types ¶
type AccessToken ¶
type AccessToken struct { gorm.Model ClientID sql.NullInt64 `sql:"index;not null"` UserID sql.NullInt64 `sql:"index"` Client *Client User *User Token string `sql:"type:varchar(40);unique;not null"` ExpiresAt time.Time `sql:"not null"` Scope string `sql:"type:varchar(200);not null"` }
AccessToken ...
func NewAccessToken ¶
func NewAccessToken(client *Client, user *User, expiresIn int, scope string) *AccessToken
NewAccessToken creates new AccessToken instance
func (*AccessToken) TableName ¶
func (at *AccessToken) TableName() string
TableName specifies table name
type AccessTokenResponse ¶
type AccessTokenResponse struct { ID uint `json:"id"` UserID uint `json:"user_id,omitempty"` AccessToken string `json:"access_token"` ExpiresIn int `json:"expires_in"` TokenType string `json:"token_type"` Scope string `json:"scope"` RefreshToken string `json:"refresh_token,omitempty"` }
AccessTokenResponse ...
type AuthorizationCode ¶
type AuthorizationCode struct { gorm.Model ClientID sql.NullInt64 `sql:"index;not null"` UserID sql.NullInt64 `sql:"index;not null"` Client *Client User *User Code string `sql:"type:varchar(40);unique;not null"` RedirectURI sql.NullString `sql:"type:varchar(200)"` ExpiresAt time.Time `sql:"not null"` Scope string `sql:"type:varchar(200);not null"` }
AuthorizationCode ...
func NewAuthorizationCode ¶
func NewAuthorizationCode(client *Client, user *User, expiresIn int, redirectURI, scope string) *AuthorizationCode
NewAuthorizationCode creates new AuthorizationCode instance
func (*AuthorizationCode) TableName ¶
func (ac *AuthorizationCode) TableName() string
TableName specifies table name
type Client ¶
type Client struct { gorm.Model Key string `sql:"type:varchar(254);unique;not null"` Secret string `sql:"type:varchar(60);not null"` RedirectURI sql.NullString `sql:"type:varchar(200)"` }
Client ...
type IntrospectResponse ¶
type IntrospectResponse struct { Active bool `json:"active"` Scope string `json:"scope,omitempty"` ClientID string `json:"client_id,omitempty"` Username string `json:"username,omitempty"` TokenType string `json:"token_type,omitempty"` ExpiresAt int `json:"exp,omitempty"` }
IntrospectResponse ...
type RefreshToken ¶
type RefreshToken struct { gorm.Model ClientID sql.NullInt64 `sql:"index;not null"` UserID sql.NullInt64 `sql:"index"` Client *Client User *User Token string `sql:"type:varchar(40);unique;not null"` ExpiresAt time.Time `sql:"not null"` Scope string `sql:"type:varchar(200);not null"` }
RefreshToken ...
func NewRefreshToken ¶
func NewRefreshToken(client *Client, user *User, expiresIn int, scope string) *RefreshToken
NewRefreshToken creates new RefreshToken instance
func (*RefreshToken) TableName ¶
func (rt *RefreshToken) TableName() string
TableName specifies table name
type Scope ¶
type Scope struct { gorm.Model Scope string `sql:"type:varchar(200);unique;not null"` Description sql.NullString IsDefault bool `sql:"default:false"` }
Scope ...
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service struct keeps objects to avoid passing them around
func NewService ¶
NewService starts a new Service instance
func (*Service) AuthClient ¶
AuthClient authenticates client
func (*Service) Authenticate ¶
func (s *Service) Authenticate(token string) (*AccessToken, error)
Authenticate checks the access token is valid
func (*Service) ClientExists ¶
ClientExists returns true if client exists
func (*Service) CreateClient ¶
CreateClient saves a new client to database
func (*Service) CreateClientTx ¶
func (s *Service) CreateClientTx(tx *gorm.DB, clientID, secret, redirectURI string) (*Client, error)
CreateClientTx saves a new client to database using injected db object
func (*Service) CreateUser ¶
CreateUser saves a new user to database
func (*Service) CreateUserTx ¶
CreateUserTx saves a new user to database using injected db object
func (*Service) FindClientByClientID ¶
FindClientByClientID looks up a client by client ID
func (*Service) FindUserByUsername ¶
FindUserByUsername looks up a user by username
func (*Service) GetOrCreateRefreshToken ¶
func (s *Service) GetOrCreateRefreshToken(client *Client, user *User, expiresIn int, scope string) (*RefreshToken, error)
GetOrCreateRefreshToken retrieves an existing refresh token, if expired, the token gets deleted and new refresh token is created
func (*Service) GetScope ¶
GetScope takes a requested scope and, if it's empty, returns the default scope, if not empty, it validates the requested scope
func (*Service) GetValidRefreshToken ¶
func (s *Service) GetValidRefreshToken(token string, client *Client) (*RefreshToken, error)
GetValidRefreshToken returns a valid non expired refresh token
func (*Service) GrantAccessToken ¶
func (s *Service) GrantAccessToken(client *Client, user *User, expiresIn int, scope string) (*AccessToken, error)
GrantAccessToken deletes old tokens and grants a new access token
func (*Service) GrantAuthorizationCode ¶
func (s *Service) GrantAuthorizationCode(client *Client, user *User, expiresIn int, redirectURI, scope string) (*AuthorizationCode, error)
GrantAuthorizationCode grants a new authorization code
func (*Service) IntrospectResponseAccessToken ¶
func (s *Service) IntrospectResponseAccessToken(at *AccessToken) *IntrospectResponse
func (*Service) IntrospectResponseRefreshToken ¶
func (s *Service) IntrospectResponseRefreshToken(rt *RefreshToken) *IntrospectResponse
func (*Service) Login ¶
func (s *Service) Login(client *Client, user *User, scope string) (*AccessToken, *RefreshToken, error)
Login creates an access token and refresh token for a user (logs him/her in)
func (*Service) SetPassword ¶
SetPassword saves a new user to database
func (*Service) UserExists ¶
UserExists returns true if user exists
type ServiceInterface ¶
type ServiceInterface interface { // Exported methods ClientExists(clientID string) bool FindClientByClientID(clientID string) (*Client, error) CreateClient(clientID, secret, redirectURI string) (*Client, error) CreateClientTx(tx *gorm.DB, clientID, secret, redirectURI string) (*Client, error) AuthClient(clientID, secret string) (*Client, error) UserExists(username string) bool FindUserByUsername(username string) (*User, error) CreateUser(username, password string) (*User, error) CreateUserTx(tx *gorm.DB, username, password string) (*User, error) SetPassword(user *User, password string) error AuthUser(username, thePassword string) (*User, error) GetScope(requestedScope string) (string, error) Login(client *Client, user *User, scope string) (*AccessToken, *RefreshToken, error) GrantAuthorizationCode(client *Client, user *User, expiresIn int, redirectURI, scope string) (*AuthorizationCode, error) GrantAccessToken(client *Client, user *User, expiresIn int, scope string) (*AccessToken, error) GetOrCreateRefreshToken(client *Client, user *User, expiresIn int, scope string) (*RefreshToken, error) GetValidRefreshToken(token string, client *Client) (*RefreshToken, error) Authenticate(token string) (*AccessToken, error) // contains filtered or unexported methods }
ServiceInterface defines exported methods
type ServiceMock ¶
ServiceMock is a mocked object implementing ServiceInterface
func (*ServiceMock) AuthClient ¶
func (_m *ServiceMock) AuthClient(clientID string, secret string) (*Client, error)
AuthClient ...
func (*ServiceMock) AuthUser ¶
func (_m *ServiceMock) AuthUser(username string, thePassword string) (*User, error)
AuthUser ...
func (*ServiceMock) Authenticate ¶
func (_m *ServiceMock) Authenticate(token string) (*AccessToken, error)
Authenticate ...
func (*ServiceMock) ClientExists ¶
func (_m *ServiceMock) ClientExists(clientID string) bool
ClientExists ...
func (*ServiceMock) CreateClient ¶
func (_m *ServiceMock) CreateClient(clientID string, secret string, redirectURI string) (*Client, error)
CreateClient ...
func (*ServiceMock) CreateClientTx ¶
func (_m *ServiceMock) CreateClientTx(tx *gorm.DB, clientID string, secret string, redirectURI string) (*Client, error)
CreateClientTx ...
func (*ServiceMock) CreateUser ¶
func (_m *ServiceMock) CreateUser(username string, password string) (*User, error)
CreateUser ...
func (*ServiceMock) CreateUserTx ¶
CreateUserTx ...
func (*ServiceMock) FindClientByClientID ¶
func (_m *ServiceMock) FindClientByClientID(clientID string) (*Client, error)
FindClientByClientID ...
func (*ServiceMock) FindUserByUsername ¶
func (_m *ServiceMock) FindUserByUsername(username string) (*User, error)
FindUserByUsername ...
func (*ServiceMock) GetOrCreateRefreshToken ¶
func (_m *ServiceMock) GetOrCreateRefreshToken(client *Client, user *User, expiresIn int, scope string) (*RefreshToken, error)
GetOrCreateRefreshToken ...
func (*ServiceMock) GetScope ¶
func (_m *ServiceMock) GetScope(requestedScope string) (string, error)
GetScope ...
func (*ServiceMock) GetValidRefreshToken ¶
func (_m *ServiceMock) GetValidRefreshToken(token string, client *Client) (*RefreshToken, error)
GetValidRefreshToken ...
func (*ServiceMock) GrantAccessToken ¶
func (_m *ServiceMock) GrantAccessToken(client *Client, user *User, expiresIn int, scope string) (*AccessToken, error)
GrantAccessToken ...
func (*ServiceMock) GrantAuthorizationCode ¶
func (_m *ServiceMock) GrantAuthorizationCode(client *Client, user *User, expiresIn int, redirectURI string, scope string) (*AuthorizationCode, error)
GrantAuthorizationCode ...
func (*ServiceMock) Login ¶
func (_m *ServiceMock) Login(client *Client, user *User, scope string) (*AccessToken, *RefreshToken, error)
Login ...
func (*ServiceMock) SetPassword ¶
func (_m *ServiceMock) SetPassword(user *User, password string) error
SetPassword ...
func (*ServiceMock) UserExists ¶
func (_m *ServiceMock) UserExists(username string) bool
UserExists ...
Source Files ¶
- access_token.go
- authenticate.go
- authorization_code.go
- client.go
- grant_type_authorization_code.go
- grant_type_client_credentials.go
- grant_type_password.go
- grant_type_refresh_token.go
- handlers.go
- introspect.go
- login.go
- migrations.go
- models.go
- refresh_token.go
- response.go
- routes.go
- scope.go
- service.go
- service_interface.go
- service_mock.go
- user.go