accounts

package
v0.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2016 License: MPL-2.0 Imports: 29 Imported by: 6

Documentation

Index

Constants

View Source
const (
	// AuthenticatedAccountKey ...
	AuthenticatedAccountKey contextKey = 0
	// AuthenticatedUserKey ...
	AuthenticatedUserKey contextKey = 1
)

Variables

View Source
var (
	// ErrAccountNotFound ...
	ErrAccountNotFound = errors.New("Account not found")
	// ErrAccountNameTaken ...
	ErrAccountNameTaken = errors.New("Account name taken")
)
View Source
var (
	// ErrAccountAuthenticationRequired ...
	ErrAccountAuthenticationRequired = errors.New("Account authentication required")
	// ErrUserAuthenticationRequired ...
	ErrUserAuthenticationRequired = errors.New("User authentication required")
)
View Source
var (
	// ErrSuperuserOnlyManually ...
	ErrSuperuserOnlyManually = errors.New("Superusers can only be created manually")
	// ErrUserNotFound ...
	ErrUserNotFound = errors.New("User not found")
)
View Source
var (
	// ErrConfirmationNotFound ...
	ErrConfirmationNotFound = errors.New("Confirmation not found")
)
View Source
var (
	// ErrGetUserPermission ...
	ErrGetUserPermission = errors.New("Need permission to get user")
)
View Source
var (
	// ErrInvitationNotFound ...
	ErrInvitationNotFound = errors.New("Invitation not found")
)
View Source
var (
	// ErrPasswordResetNotFound ...
	ErrPasswordResetNotFound = errors.New("Password reset not found")
)
View Source
var (
	// ErrRoleNotFound ...
	ErrRoleNotFound = errors.New("Role not found")
)
View Source
var (
	// ErrUpdateUserPermission ...
	ErrUpdateUserPermission = errors.New("Need permission to update user")
)

Functions

func AccountPreload

func AccountPreload(db *gorm.DB) *gorm.DB

AccountPreload sets up Gorm preloads for an account object

func AccountPreloadWithPrefix

func AccountPreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB

AccountPreloadWithPrefix sets up Gorm preloads for an account object, and prefixes with prefix for nested objects

func ConfirmationPreload

func ConfirmationPreload(db *gorm.DB) *gorm.DB

ConfirmationPreload sets up Gorm preloads for a confirmation object

func ConfirmationPreloadWithPrefix

func ConfirmationPreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB

ConfirmationPreloadWithPrefix sets up Gorm preloads for a confirmation object, and prefixes with prefix for nested objects

func InvitationPreload

func InvitationPreload(db *gorm.DB) *gorm.DB

InvitationPreload sets up Gorm preloads for an invitation object

func InvitationPreloadWithPrefix

func InvitationPreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB

InvitationPreloadWithPrefix sets up Gorm preloads for an invitation object, and prefixes with prefix for nested objects

func MigrateAll

func MigrateAll(db *gorm.DB) error

MigrateAll executes all migrations

func PasswordResetPreload

func PasswordResetPreload(db *gorm.DB) *gorm.DB

PasswordResetPreload sets up Gorm preloads for a password reset object

func PasswordResetPreloadWithPrefix

func PasswordResetPreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB

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

func UserPreload(db *gorm.DB) *gorm.DB

UserPreload sets up Gorm preloads for a user object

func UserPreloadWithPrefix

func UserPreloadWithPrefix(db *gorm.DB, prefix string) *gorm.DB

UserPreloadWithPrefix sets up Gorm preloads for a user object, and prefixes with prefix for nested objects

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

func GetAuthenticatedAccount(r *http.Request) (*Account, error)

GetAuthenticatedAccount returns *Account from the request context

func NewAccount

func NewAccount(oauthClient *oauth.Client, name, description string) *Account

NewAccount creates new Account instance

func (*Account) TableName

func (p *Account) TableName() string

TableName specifies table name

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

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

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)

func (*Role) TableName

func (r *Role) TableName() string

TableName specifies table name

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

func (s *Service) ConfirmUser(user *User) error

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

func (s *Service) CreateSuperuser(account *Account, email, password string) (*User, error)

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

func (s *Service) FindAccountByID(accountID uint) (*Account, error)

FindAccountByID looks up an account by ID and returns it

func (*Service) FindAccountByName

func (s *Service) FindAccountByName(name string) (*Account, error)

FindAccountByName looks up an account by name and returns it

func (*Service) FindAccountByOauthClientID

func (s *Service) FindAccountByOauthClientID(oauthClientID uint) (*Account, error)

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

func (s *Service) FindRoleByID(id string) (*Role, error)

FindRoleByID looks up a role by ID and returns it

func (*Service) FindUserByEmail

func (s *Service) FindUserByEmail(email string) (*User, error)

FindUserByEmail looks up a user by email and returns it

func (*Service) FindUserByFacebookID

func (s *Service) FindUserByFacebookID(facebookID string) (*User, error)

FindUserByFacebookID looks up a user by a Facebook ID and returns it

func (*Service) FindUserByID

func (s *Service) FindUserByID(userID uint) (*User, error)

FindUserByID looks up a user by ID and returns it

func (*Service) FindUserByOauthUserID

func (s *Service) FindUserByOauthUserID(oauthUserID uint) (*User, error)

FindUserByOauthUserID looks up a user by oauth user ID and returns it

func (*Service) GetClientCredentials

func (s *Service) GetClientCredentials(r *http.Request) (*Account, *User, error)

GetClientCredentials ...

func (*Service) GetConfig

func (s *Service) GetConfig() *config.Config

GetConfig returns config.Config instance

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

func (s *Service) GetUserCredentials(token string) (*Account, *User, error)

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

func GetAuthenticatedUser(r *http.Request) (*User, error)

GetAuthenticatedUser returns *User from the request context

func NewUser

func NewUser(account *Account, oauthUser *oauth.User, role *Role, facebookID string, confirmed bool, data *UserRequest) *User

NewUser creates new User instance

func (*User) GetName

func (u *User) GetName() string

GetName returns user's full name

func (*User) TableName

func (u *User) TableName() string

TableName specifies table name

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

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL