auth

Paddy 2014-10-15 Child:cfab12566289

50:b620d32d9903 Go to Latest

auth/context.go

Create the Context type and its helpers. Create a Context type that ties together all our Stores and other configuration-specific items. Create helper functions for the Context, to throw errors when something is used without first being set, as all possible Context values _can_ be nil. Basically, it's better to throw an error than panic.

History
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/context.go	Wed Oct 15 23:23:53 2014 -0400
     1.3 @@ -0,0 +1,211 @@
     1.4 +package auth
     1.5 +
     1.6 +import (
     1.7 +	"errors"
     1.8 +	"html/template"
     1.9 +	"io"
    1.10 +	"time"
    1.11 +
    1.12 +	"code.secondbit.org/uuid"
    1.13 +)
    1.14 +
    1.15 +var (
    1.16 +	ErrNoTemplate = errors.New("no Template specified for the Context")
    1.17 +)
    1.18 +
    1.19 +type Context struct {
    1.20 +	template *template.Template
    1.21 +	clients  ClientStore
    1.22 +	grants   GrantStore
    1.23 +	profiles ProfileStore
    1.24 +	tokens   TokenStore
    1.25 +}
    1.26 +
    1.27 +func (c Context) Render(out io.Writer, name string, data interface{}) error {
    1.28 +	if c.template == nil {
    1.29 +		return ErrNoTemplate
    1.30 +	}
    1.31 +	return c.template.ExecuteTemplate(out, name, data)
    1.32 +}
    1.33 +
    1.34 +func (c Context) GetClient(id uuid.ID) (Client, error) {
    1.35 +	if c.clients == nil {
    1.36 +		return Client{}, ErrNoClientStore
    1.37 +	}
    1.38 +	return c.clients.GetClient(id)
    1.39 +}
    1.40 +
    1.41 +func (c Context) SaveClient(client Client) error {
    1.42 +	if c.clients == nil {
    1.43 +		return ErrNoClientStore
    1.44 +	}
    1.45 +	return c.clients.SaveClient(client)
    1.46 +}
    1.47 +
    1.48 +func (c Context) UpdateClient(id uuid.ID, change ClientChange) error {
    1.49 +	if c.clients == nil {
    1.50 +		return ErrNoClientStore
    1.51 +	}
    1.52 +	return c.clients.UpdateClient(id, change)
    1.53 +}
    1.54 +
    1.55 +func (c Context) DeleteClient(id uuid.ID) error {
    1.56 +	if c.clients == nil {
    1.57 +		return ErrNoClientStore
    1.58 +	}
    1.59 +	return c.clients.DeleteClient(id)
    1.60 +}
    1.61 +
    1.62 +func (c Context) ListClientsByOwner(ownerID uuid.ID, num, offset int) ([]Client, error) {
    1.63 +	if c.clients == nil {
    1.64 +		return []Client{}, ErrNoClientStore
    1.65 +	}
    1.66 +	return c.clients.ListClientsByOwner(ownerID, num, offset)
    1.67 +}
    1.68 +
    1.69 +func (c Context) AddEndpoint(client uuid.ID, endpoint Endpoint) error {
    1.70 +	if c.clients == nil {
    1.71 +		return ErrNoClientStore
    1.72 +	}
    1.73 +	return c.clients.AddEndpoint(client, endpoint)
    1.74 +}
    1.75 +
    1.76 +func (c Context) RemoveEndpoint(client, endpoint uuid.ID) error {
    1.77 +	if c.clients == nil {
    1.78 +		return ErrNoClientStore
    1.79 +	}
    1.80 +	return c.clients.RemoveEndpoint(client, endpoint)
    1.81 +}
    1.82 +
    1.83 +func (c Context) CheckEndpoint(client uuid.ID, URI string) (bool, error) {
    1.84 +	if c.clients == nil {
    1.85 +		return false, ErrNoClientStore
    1.86 +	}
    1.87 +	return c.clients.CheckEndpoint(client, URI)
    1.88 +}
    1.89 +
    1.90 +func (c Context) ListEndpoints(client uuid.ID, num, offset int) ([]Endpoint, error) {
    1.91 +	if c.clients == nil {
    1.92 +		return []Endpoint{}, ErrNoClientStore
    1.93 +	}
    1.94 +	return c.clients.ListEndpoints(client, num, offset)
    1.95 +}
    1.96 +
    1.97 +func (c Context) GetGrant(code string) (Grant, error) {
    1.98 +	if c.grants == nil {
    1.99 +		return Grant{}, ErrNoGrantStore
   1.100 +	}
   1.101 +	return c.grants.GetGrant(code)
   1.102 +}
   1.103 +
   1.104 +func (c Context) SaveGrant(grant Grant) error {
   1.105 +	if c.grants == nil {
   1.106 +		return ErrNoGrantStore
   1.107 +	}
   1.108 +	return c.grants.SaveGrant(grant)
   1.109 +}
   1.110 +
   1.111 +func (c Context) DeleteGrant(code string) error {
   1.112 +	if c.grants == nil {
   1.113 +		return ErrNoGrantStore
   1.114 +	}
   1.115 +	return c.grants.DeleteGrant(code)
   1.116 +}
   1.117 +
   1.118 +func (c Context) GetProfileByID(id uuid.ID) (Profile, error) {
   1.119 +	if c.profiles == nil {
   1.120 +		return Profile{}, ErrNoProfileStore
   1.121 +	}
   1.122 +	return c.profiles.GetProfileByID(id)
   1.123 +}
   1.124 +
   1.125 +func (c Context) GetProfileByLogin(loginType, value string) (Profile, error) {
   1.126 +	if c.profiles == nil {
   1.127 +		return Profile{}, ErrNoProfileStore
   1.128 +	}
   1.129 +	return c.profiles.GetProfileByLogin(loginType, value)
   1.130 +}
   1.131 +
   1.132 +func (c Context) SaveProfile(profile Profile) error {
   1.133 +	if c.profiles == nil {
   1.134 +		return ErrNoProfileStore
   1.135 +	}
   1.136 +	return c.profiles.SaveProfile(profile)
   1.137 +}
   1.138 +
   1.139 +func (c Context) UpdateProfile(id uuid.ID, change ProfileChange) error {
   1.140 +	if c.profiles == nil {
   1.141 +		return ErrNoProfileStore
   1.142 +	}
   1.143 +	return c.profiles.UpdateProfile(id, change)
   1.144 +}
   1.145 +
   1.146 +func (c Context) UpdateProfiles(ids []uuid.ID, change BulkProfileChange) error {
   1.147 +	if c.profiles == nil {
   1.148 +		return ErrNoProfileStore
   1.149 +	}
   1.150 +	return c.profiles.UpdateProfiles(ids, change)
   1.151 +}
   1.152 +
   1.153 +func (c Context) DeleteProfile(id uuid.ID) error {
   1.154 +	if c.profiles == nil {
   1.155 +		return ErrNoProfileStore
   1.156 +	}
   1.157 +	return c.profiles.DeleteProfile(id)
   1.158 +}
   1.159 +
   1.160 +func (c Context) AddLogin(login Login) error {
   1.161 +	if c.profiles == nil {
   1.162 +		return ErrNoProfileStore
   1.163 +	}
   1.164 +	return c.profiles.AddLogin(login)
   1.165 +}
   1.166 +
   1.167 +func (c Context) RemoveLogin(loginType, value string, profile uuid.ID) error {
   1.168 +	if c.profiles == nil {
   1.169 +		return ErrNoProfileStore
   1.170 +	}
   1.171 +	return c.profiles.RemoveLogin(loginType, value, profile)
   1.172 +}
   1.173 +
   1.174 +func (c Context) RecordLoginUse(loginType, value string, when time.Time) error {
   1.175 +	if c.profiles == nil {
   1.176 +		return ErrNoProfileStore
   1.177 +	}
   1.178 +	return c.profiles.RecordLoginUse(loginType, value, when)
   1.179 +}
   1.180 +
   1.181 +func (c Context) ListLogins(profile uuid.ID, num, offset int) ([]Login, error) {
   1.182 +	if c.profiles == nil {
   1.183 +		return []Login{}, ErrNoProfileStore
   1.184 +	}
   1.185 +	return c.profiles.ListLogins(profile, num, offset)
   1.186 +}
   1.187 +
   1.188 +func (c Context) GetToken(token string, refresh bool) (Token, error) {
   1.189 +	if c.tokens == nil {
   1.190 +		return Token{}, ErrNoTokenStore
   1.191 +	}
   1.192 +	return c.tokens.GetToken(token, refresh)
   1.193 +}
   1.194 +
   1.195 +func (c Context) SaveToken(token Token) error {
   1.196 +	if c.tokens == nil {
   1.197 +		return ErrNoTokenStore
   1.198 +	}
   1.199 +	return c.tokens.SaveToken(token)
   1.200 +}
   1.201 +
   1.202 +func (c Context) RemoveToken(token string) error {
   1.203 +	if c.tokens == nil {
   1.204 +		return ErrNoTokenStore
   1.205 +	}
   1.206 +	return c.tokens.RemoveToken(token)
   1.207 +}
   1.208 +
   1.209 +func (c Context) GetTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error) {
   1.210 +	if c.tokens == nil {
   1.211 +		return []Token{}, ErrNoTokenStore
   1.212 +	}
   1.213 +	return c.tokens.GetTokensByProfileID(profileID, num, offset)
   1.214 +}