Documentation ¶
Index ¶
- Constants
- Variables
- func AccountPreload(db *gorm.DB) *gorm.DB
- func AccountPreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB
- func ConfirmationPreload(db *gorm.DB) *gorm.DB
- func ConfirmationPreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB
- func InvitationPreload(db *gorm.DB) *gorm.DB
- func InvitationPreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB
- func MigrateAll(db *gorm.DB) error
- func PasswordResetPreload(db *gorm.DB) *gorm.DB
- func PasswordResetPreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB
- func RegisterRoutes(router *mux.Router, service ServiceInterface)
- func UserPreload(db *gorm.DB) *gorm.DB
- func UserPreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB
- type Account
- type AccountAuthMiddleware
- type ConfirmInvitationRequest
- type ConfirmPasswordResetRequest
- type Confirmation
- type EmailFactory
- type EmailFactoryInterface
- type Invitation
- type InvitationRequest
- type OptionalUserAuthMiddleware
- type PasswordRequest
- type PasswordReset
- type PasswordResetRequest
- type Role
- type Service
- func (s *Service) ConfirmEmailHandler(w http.ResponseWriter, r *http.Request)
- func (s *Service) ConfirmInvitation(invitation *Invitation, password string) error
- func (s *Service) ConfirmInvitationHandler(w http.ResponseWriter, r *http.Request)
- func (s *Service) ConfirmPasswordResetHandler(w http.ResponseWriter, r *http.Request)
- func (s *Service) ConfirmUser(user *User) error
- func (s *Service) CreateAccount(name, description, key, secret, redirectURI string) (*Account, error)
- func (s *Service) CreatePasswordResetHandler(w http.ResponseWriter, r *http.Request)
- func (s *Service) CreateSuperuser(account *Account, email, password string) (*User, error)
- func (s *Service) CreateUser(account *Account, userRequest *UserRequest) (*User, error)
- func (s *Service) CreateUserHandler(w http.ResponseWriter, r *http.Request)
- func (s *Service) CreateUserTx(tx *gorm.DB, account *Account, userRequest *UserRequest) (*User, error)
- func (s *Service) FindAccountByID(accountID uint) (*Account, error)
- func (s *Service) FindAccountByName(name string) (*Account, error)
- func (s *Service) FindAccountByOauthClientID(oauthClientID uint) (*Account, error)
- func (s *Service) FindConfirmationByReference(reference string) (*Confirmation, error)
- func (s *Service) FindInvitationByID(id uint) (*Invitation, error)
- func (s *Service) FindInvitationByReference(reference string) (*Invitation, error)
- func (s *Service) FindPasswordResetByReference(reference string) (*PasswordReset, error)
- func (s *Service) FindRoleByID(id string) (*Role, error)
- func (s *Service) FindUserByEmail(email string) (*User, error)
- func (s *Service) FindUserByFacebookID(facebookID string) (*User, error)
- func (s *Service) FindUserByID(userID uint) (*User, error)
- func (s *Service) FindUserByOauthUserID(oauthUserID uint) (*User, error)
- func (s *Service) GetClientCredentials(r *http.Request) (*Account, *User, error)
- func (s *Service) GetConfig() *config.Config
- func (s *Service) GetMyUserHandler(w http.ResponseWriter, r *http.Request)
- func (s *Service) GetOauthService() oauth.ServiceInterface
- func (s *Service) GetOrCreateFacebookUser(account *Account, facebookID string, userRequest *UserRequest) (*User, error)
- func (s *Service) GetUserCredentials(token string) (*Account, *User, error)
- func (s *Service) GetUserHandler(w http.ResponseWriter, r *http.Request)
- func (s *Service) InviteUser(invitedByUser *User, invitationRequest *InvitationRequest) (*Invitation, error)
- func (s *Service) InviteUserHandler(w http.ResponseWriter, r *http.Request)
- func (s *Service) InviteUserTx(tx *gorm.DB, invitedByUser *User, invitationRequest *InvitationRequest) (*Invitation, error)
- func (s *Service) ResetPassword(passwordReset *PasswordReset, password string) error
- func (s *Service) UpdateUser(user *User, userRequest *UserRequest) error
- func (s *Service) UpdateUserHandler(w http.ResponseWriter, r *http.Request)
- type ServiceInterface
- type User
- type UserAuthMiddleware
- type UserRequest
- type UserResponse
Constants ¶
const ( // AuthenticatedAccountKey ... AuthenticatedAccountKey contextKey = 0 // AuthenticatedUserKey ... AuthenticatedUserKey contextKey = 1 )
Variables ¶
var ( // ErrAccountNotFound ... ErrAccountNotFound = errors.New("Account not found") // ErrAccountNameTaken ... ErrAccountNameTaken = errors.New("Account name taken") )
var ( // ErrAccountAuthenticationRequired ... ErrAccountAuthenticationRequired = errors.New("Account authentication required") // ErrUserAuthenticationRequired ... ErrUserAuthenticationRequired = errors.New("User authentication required") )
var ( // ErrSuperuserOnlyManually ... ErrSuperuserOnlyManually = errors.New("Superusers can only be created manually") // ErrUserNotFound ... ErrUserNotFound = errors.New("User not found") )
var ( // ErrConfirmationNotFound ... ErrConfirmationNotFound = errors.New("Confirmation not found") )
var ( // ErrGetUserPermission ... ErrGetUserPermission = errors.New("Need permission to get user") )
var ( // ErrInvitationNotFound ... ErrInvitationNotFound = errors.New("Invitation not found") )
var ( // ErrPasswordResetNotFound ... ErrPasswordResetNotFound = errors.New("Password reset not found") )
var ( // ErrRoleNotFound ... ErrRoleNotFound = errors.New("Role not found") )
var ( // ErrUpdateUserPermission ... ErrUpdateUserPermission = errors.New("Need permission to update user") )
Functions ¶
func AccountPreload ¶
AccountPreload sets up Gorm preloads for an account object
func AccountPreloadWithPrefix ¶
AccountPreloadWithPrefix sets up Gorm preloads for an account object, and prefixes with prefix for nested objects
func ConfirmationPreload ¶
ConfirmationPreload sets up Gorm preloads for a confirmation object
func ConfirmationPreloadWithPrefix ¶
ConfirmationPreloadWithPrefix sets up Gorm preloads for a confirmation object, and prefixes with prefix for nested objects
func InvitationPreload ¶
InvitationPreload sets up Gorm preloads for an invitation object
func InvitationPreloadWithPrefix ¶
InvitationPreloadWithPrefix sets up Gorm preloads for an invitation object, and prefixes with prefix for nested objects
func PasswordResetPreload ¶
PasswordResetPreload sets up Gorm preloads for a password reset object
func PasswordResetPreloadWithPrefix ¶
PasswordResetPreloadWithPrefix sets up Gorm preloads for a password reset object, and prefixes with prefix for nested objects
func RegisterRoutes ¶
func RegisterRoutes(router *mux.Router, service ServiceInterface)
RegisterRoutes registers route handlers for the accounts service
func UserPreload ¶
UserPreload sets up Gorm preloads for a user object
Types ¶
type Account ¶
type Account struct { gorm.Model OauthClientID sql.NullInt64 `sql:"index;not null"` OauthClient *oauth.Client Name string `sql:"type:varchar(100);unique;not null"` Description sql.NullString `sql:"type:varchar(200)"` }
Account represents an extension of Oauth 2.0 client, can be used to group users together
func GetAuthenticatedAccount ¶
GetAuthenticatedAccount returns *Account from the request context
func NewAccount ¶
NewAccount creates new Account instance
type AccountAuthMiddleware ¶
type AccountAuthMiddleware struct {
// contains filtered or unexported fields
}
AccountAuthMiddleware takes the client ID and secret from the basic auth, authenticates the account and sets the account object on the request context
func NewAccountAuthMiddleware ¶
func NewAccountAuthMiddleware(service ServiceInterface) *AccountAuthMiddleware
NewAccountAuthMiddleware creates a new AccountAuthMiddleware instance
func (*AccountAuthMiddleware) ServeHTTP ¶
func (m *AccountAuthMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)
ServeHTTP as per the negroni.Handler interface
type ConfirmInvitationRequest ¶
type ConfirmInvitationRequest struct {
PasswordRequest
}
ConfirmInvitationRequest ...
type ConfirmPasswordResetRequest ¶
type ConfirmPasswordResetRequest struct {
PasswordRequest
}
ConfirmPasswordResetRequest ...
type Confirmation ¶
type Confirmation struct { gorm.Model UserID sql.NullInt64 `sql:"index;not null"` User *User Reference string `sql:"type:varchar(40);unique;not null"` EmailSent bool `sql:"index;not null"` EmailSentAt pq.NullTime }
Confirmation objects is created when we send user a confirmation email It is then fetched when user clicks on the verification link in the email so we can verify his/her email
func NewConfirmation ¶
func NewConfirmation(user *User) *Confirmation
NewConfirmation creates new Confirmation instance
func (*Confirmation) TableName ¶
func (c *Confirmation) TableName() string
TableName specifies table name
type EmailFactory ¶
type EmailFactory struct {
// contains filtered or unexported fields
}
EmailFactory facilitates construction of email.Email objects
func NewEmailFactory ¶
func NewEmailFactory(cnf *config.Config) *EmailFactory
NewEmailFactory starts a new emailFactory instance
func (*EmailFactory) NewConfirmationEmail ¶
func (f *EmailFactory) NewConfirmationEmail(confirmation *Confirmation) (*email.Message, error)
NewConfirmationEmail returns a confirmation email
func (*EmailFactory) NewInvitationEmail ¶
func (f *EmailFactory) NewInvitationEmail(invitation *Invitation) (*email.Message, error)
NewInvitationEmail returns a user invite email
func (*EmailFactory) NewPasswordResetEmail ¶
func (f *EmailFactory) NewPasswordResetEmail(passwordReset *PasswordReset) (*email.Message, error)
NewPasswordResetEmail returns a password reset email
type EmailFactoryInterface ¶
type EmailFactoryInterface interface { NewConfirmationEmail(confirmation *Confirmation) (*email.Message, error) NewInvitationEmail(invitation *Invitation) (*email.Message, error) NewPasswordResetEmail(passwordReset *PasswordReset) (*email.Message, error) }
EmailFactoryInterface defines exported methods
type Invitation ¶
type Invitation struct { gorm.Model InvitedUserID sql.NullInt64 `sql:"index;not null"` InvitedByUserID sql.NullInt64 `sql:"index;not null"` InvitedByUser *User InvitedUser *User Reference string `sql:"type:varchar(40);unique;not null"` EmailSent bool `sql:"index;not null"` EmailSentAt pq.NullTime }
Invitation is created when user invites another user to the platform. We send out an invite email and the invited user can follow the link to set a password and finish the sign up process
func NewInvitation ¶
func NewInvitation(invitedUser, invitedByUser *User) *Invitation
NewInvitation creates new Invitation instance
func (*Invitation) TableName ¶
func (i *Invitation) TableName() string
TableName specifies table name
type InvitationRequest ¶
type InvitationRequest struct { Email string `json:"email"` FirstName string `json:"first_name"` LastName string `json:"last_name"` }
InvitationRequest ...
type OptionalUserAuthMiddleware ¶
type OptionalUserAuthMiddleware struct {
// contains filtered or unexported fields
}
OptionalUserAuthMiddleware takes the bearer token from the Authorization header, authenticates the user and sets the user object on the request context. If it cannot find it, it just continues
func NewOptionalUserAuthMiddleware ¶
func NewOptionalUserAuthMiddleware(service ServiceInterface) *OptionalUserAuthMiddleware
NewOptionalUserAuthMiddleware creates a new OptionalUserAuthMiddleware instance
func (*OptionalUserAuthMiddleware) ServeHTTP ¶
func (m *OptionalUserAuthMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)
ServeHTTP as per the negroni.Handler interface
type PasswordRequest ¶
type PasswordRequest struct {
Password string `json:"password"`
}
PasswordRequest ...
type PasswordReset ¶
type PasswordReset struct { gorm.Model UserID sql.NullInt64 `sql:"index;not null"` User *User Reference string `sql:"type:varchar(40);unique;not null"` EmailSent bool `sql:"index;not null"` EmailSentAt pq.NullTime }
PasswordReset is created when user forgets his/her password and requests a new one. We send out an email with a link where user can set a new password.
func NewPasswordReset ¶
func NewPasswordReset(user *User) *PasswordReset
NewPasswordReset creates new PasswordReset instance
func (*PasswordReset) TableName ¶
func (p *PasswordReset) TableName() string
TableName specifies table name
type PasswordResetRequest ¶
type PasswordResetRequest struct {
Email string `json:"email"`
}
PasswordResetRequest ...
type Role ¶
type Role struct { database.TimestampModel ID string `gorm:"primary_key" sql:"type:varchar(20)"` Name string `sql:"type:varchar(50);unique;not null"` }
Role is a one of roles user can have (currently superuser or user)
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service struct keeps config and db objects to avoid passing them around
func NewService ¶
func NewService(cnf *config.Config, db *gorm.DB, oauthService oauth.ServiceInterface, emailService email.ServiceInterface, emailFactory EmailFactoryInterface) *Service
NewService starts a new Service instance
func (*Service) ConfirmEmailHandler ¶
func (s *Service) ConfirmEmailHandler(w http.ResponseWriter, r *http.Request)
ConfirmEmailHandler - requests to confirm user's email based on a reference string
func (*Service) ConfirmInvitation ¶
func (s *Service) ConfirmInvitation(invitation *Invitation, password string) error
ConfirmInvitation sets password on the oauth user object and deletes the invitation
func (*Service) ConfirmInvitationHandler ¶
func (s *Service) ConfirmInvitationHandler(w http.ResponseWriter, r *http.Request)
ConfirmInvitationHandler - requests to complete an invitation of a user by setting password
func (*Service) ConfirmPasswordResetHandler ¶
func (s *Service) ConfirmPasswordResetHandler(w http.ResponseWriter, r *http.Request)
ConfirmPasswordResetHandler - requests to complete a password reset by setting new password
func (*Service) ConfirmUser ¶
ConfirmUser sets confirmed flag to true
func (*Service) CreateAccount ¶
func (s *Service) CreateAccount(name, description, key, secret, redirectURI string) (*Account, error)
CreateAccount creates a new account
func (*Service) CreatePasswordResetHandler ¶
func (s *Service) CreatePasswordResetHandler(w http.ResponseWriter, r *http.Request)
CreatePasswordResetHandler - requests to reset a password (POST /v1/accounts/passwordreset)
func (*Service) CreateSuperuser ¶
CreateSuperuser creates a new superuser account
func (*Service) CreateUser ¶
func (s *Service) CreateUser(account *Account, userRequest *UserRequest) (*User, error)
CreateUser creates a new oauth user and a new account user
func (*Service) CreateUserHandler ¶
func (s *Service) CreateUserHandler(w http.ResponseWriter, r *http.Request)
CreateUserHandler - requests to create a new user (POST /v1/accounts/users)
func (*Service) CreateUserTx ¶
func (s *Service) CreateUserTx(tx *gorm.DB, account *Account, userRequest *UserRequest) (*User, error)
CreateUserTx creates a new oauth user and a new account user in a transaction
func (*Service) FindAccountByID ¶
FindAccountByID looks up an account by ID and returns it
func (*Service) FindAccountByName ¶
FindAccountByName looks up an account by name and returns it
func (*Service) FindAccountByOauthClientID ¶
FindAccountByOauthClientID looks up an account by oauth client ID and returns it
func (*Service) FindConfirmationByReference ¶
func (s *Service) FindConfirmationByReference(reference string) (*Confirmation, error)
FindConfirmationByReference looks up a confirmation by a reference
func (*Service) FindInvitationByID ¶
func (s *Service) FindInvitationByID(id uint) (*Invitation, error)
FindInvitationByID looks up an invitation by ID and returns it
func (*Service) FindInvitationByReference ¶
func (s *Service) FindInvitationByReference(reference string) (*Invitation, error)
FindInvitationByReference looks up an invitation by a reference and returns it
func (*Service) FindPasswordResetByReference ¶
func (s *Service) FindPasswordResetByReference(reference string) (*PasswordReset, error)
FindPasswordResetByReference looks up a password reset by a reference
func (*Service) FindRoleByID ¶
FindRoleByID looks up a role by ID and returns it
func (*Service) FindUserByEmail ¶
FindUserByEmail looks up a user by email and returns it
func (*Service) FindUserByFacebookID ¶
FindUserByFacebookID looks up a user by a Facebook ID and returns it
func (*Service) FindUserByID ¶
FindUserByID looks up a user by ID and returns it
func (*Service) FindUserByOauthUserID ¶
FindUserByOauthUserID looks up a user by oauth user ID and returns it
func (*Service) GetClientCredentials ¶
GetClientCredentials ...
func (*Service) GetMyUserHandler ¶
func (s *Service) GetMyUserHandler(w http.ResponseWriter, r *http.Request)
GetMyUserHandler - requests to get own user data (GET /v1/accounts/me)
func (*Service) GetOauthService ¶
func (s *Service) GetOauthService() oauth.ServiceInterface
GetOauthService returns oauth.Service instance
func (*Service) GetOrCreateFacebookUser ¶
func (s *Service) GetOrCreateFacebookUser(account *Account, facebookID string, userRequest *UserRequest) (*User, error)
GetOrCreateFacebookUser either returns an existing user or updates an existing email user with facebook ID or creates a new user
func (*Service) GetUserCredentials ¶
GetUserCredentials ...
func (*Service) GetUserHandler ¶
func (s *Service) GetUserHandler(w http.ResponseWriter, r *http.Request)
GetUserHandler - requests to get a user (GET /v1/accounts/users/{id:[0-9]+})
func (*Service) InviteUser ¶
func (s *Service) InviteUser(invitedByUser *User, invitationRequest *InvitationRequest) (*Invitation, error)
InviteUser invites a new user and sends an invitation email
func (*Service) InviteUserHandler ¶
func (s *Service) InviteUserHandler(w http.ResponseWriter, r *http.Request)
InviteUserHandler - requests to invite a new user (POST /v1/accounts/invitations)
func (*Service) InviteUserTx ¶
func (s *Service) InviteUserTx(tx *gorm.DB, invitedByUser *User, invitationRequest *InvitationRequest) (*Invitation, error)
InviteUserTx invites a new user and sends an invitation email in a transaction
func (*Service) ResetPassword ¶
func (s *Service) ResetPassword(passwordReset *PasswordReset, password string) error
ResetPassword sets a new password and deletes the password reset record
func (*Service) UpdateUser ¶
func (s *Service) UpdateUser(user *User, userRequest *UserRequest) error
UpdateUser updates an existing user
func (*Service) UpdateUserHandler ¶
func (s *Service) UpdateUserHandler(w http.ResponseWriter, r *http.Request)
UpdateUserHandler - requests to update a user (PUT /v1/accounts/users/{id:[0-9]+})
type ServiceInterface ¶
type ServiceInterface interface { // Exported methods GetConfig() *config.Config GetOauthService() oauth.ServiceInterface FindAccountByOauthClientID(oauthClientID uint) (*Account, error) FindAccountByID(accountID uint) (*Account, error) FindAccountByName(name string) (*Account, error) CreateAccount(name, description, key, secret, redirectURI string) (*Account, error) FindUserByOauthUserID(oauthUserID uint) (*User, error) FindUserByEmail(email string) (*User, error) FindUserByID(userID uint) (*User, error) FindUserByFacebookID(facebookID string) (*User, error) CreateUser(account *Account, userRequest *UserRequest) (*User, error) CreateUserTx(tx *gorm.DB, account *Account, userRequest *UserRequest) (*User, error) UpdateUser(user *User, userRequest *UserRequest) error FindConfirmationByReference(reference string) (*Confirmation, error) ConfirmUser(user *User) error FindPasswordResetByReference(reference string) (*PasswordReset, error) ResetPassword(passwordReset *PasswordReset, password string) error GetOrCreateFacebookUser(account *Account, facebookID string, userRequest *UserRequest) (*User, error) CreateSuperuser(account *Account, email, password string) (*User, error) FindInvitationByID(invitationID uint) (*Invitation, error) FindInvitationByReference(reference string) (*Invitation, error) InviteUser(invitedByUser *User, invitationRequest *InvitationRequest) (*Invitation, error) InviteUserTx(tx *gorm.DB, invitedByUser *User, invitationRequest *InvitationRequest) (*Invitation, error) ConfirmInvitation(invitation *Invitation, password string) error GetUserCredentials(token string) (*Account, *User, error) GetClientCredentials(r *http.Request) (*Account, *User, error) // Needed for the NewRoutes to be able to register handlers CreateUserHandler(w http.ResponseWriter, r *http.Request) GetMyUserHandler(w http.ResponseWriter, r *http.Request) GetUserHandler(w http.ResponseWriter, r *http.Request) UpdateUserHandler(w http.ResponseWriter, r *http.Request) InviteUserHandler(w http.ResponseWriter, r *http.Request) CreatePasswordResetHandler(w http.ResponseWriter, r *http.Request) ConfirmEmailHandler(w http.ResponseWriter, r *http.Request) ConfirmInvitationHandler(w http.ResponseWriter, r *http.Request) ConfirmPasswordResetHandler(w http.ResponseWriter, r *http.Request) }
ServiceInterface defines exported methods
type User ¶
type User struct { gorm.Model AccountID sql.NullInt64 `sql:"index;not null"` OauthUserID sql.NullInt64 `sql:"index;not null"` RoleID sql.NullString `sql:"type:varchar(20);index;not null"` Account *Account OauthUser *oauth.User Role *Role FacebookID sql.NullString `sql:"type:varchar(60);unique"` FirstName sql.NullString `sql:"type:varchar(100)"` LastName sql.NullString `sql:"type:varchar(100)"` Picture sql.NullString `sql:"type:varchar(255)"` Confirmed bool `sql:"index;not null"` }
User represents a platform user
func GetAuthenticatedUser ¶
GetAuthenticatedUser returns *User from the request context
type UserAuthMiddleware ¶
type UserAuthMiddleware struct {
// contains filtered or unexported fields
}
UserAuthMiddleware takes the bearer token from the Authorization header, authenticates the user and sets the user object on the request context. If it cannot find it then it throws an unauthorized error
func NewUserAuthMiddleware ¶
func NewUserAuthMiddleware(service ServiceInterface) *UserAuthMiddleware
NewUserAuthMiddleware creates a new UserAuthMiddleware instance
func (*UserAuthMiddleware) ServeHTTP ¶
func (m *UserAuthMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)
ServeHTTP as per the negroni.Handler interface
type UserRequest ¶
type UserRequest struct { Email string `json:"email"` Password string `json:"password"` NewPassword string `json:"new_password"` FirstName string `json:"first_name"` LastName string `json:"last_name"` Picture string `json:"picture"` Role string `json:"role"` }
UserRequest ...
type UserResponse ¶
type UserResponse struct { jsonhal.Hal ID uint `json:"id"` Email string `json:"email"` FirstName string `json:"first_name"` LastName string `json:"last_name"` Picture string `json:"picture"` Role string `json:"role"` Confirmed bool `json:"confirmed"` CreatedAt string `json:"created_at"` UpdatedAt string `json:"updated_at"` }
UserResponse ...
func NewUserResponse ¶
func NewUserResponse(user *User) (*UserResponse, error)
NewUserResponse creates new UserResponse instance
Source Files ¶
- account.go
- account_auth_middleware.go
- confirm_email.go
- confirm_invitation.go
- confirm_password_reset.go
- confirmation.go
- context.go
- create_password_reset.go
- create_user.go
- email_factory.go
- email_factory_interface.go
- errors.go
- get_my_user.go
- get_user.go
- invitation.go
- invite_user.go
- logger.go
- migrations.go
- models.go
- password_reset.go
- request.go
- request_credentials.go
- response.go
- role.go
- routes.go
- service.go
- service_interface.go
- update_user.go
- user.go
- user_auth_middleware.go