auth

Paddy 2014-09-18 Parent:1f7b44b130a0 Child:fb827644bfd8

40:0b86c2d3ec75 Go to Latest

auth/profile.go

Add TODOs. Add TODO comments where functionality is stubbed out or anticipated, but work is needed to implement it.

History
1 package auth
3 import (
4 "errors"
5 "time"
7 "secondbit.org/uuid"
8 )
10 var (
11 ErrProfileAlreadyExists = errors.New("profile already exists in ProfileStore")
12 ErrProfileNotFound = errors.New("profile not found in ProfileStore")
13 )
15 type Profile struct {
16 ID uuid.ID
17 Name string
18 Passphrase string
19 Iterations int64
20 Salt string
21 PassphraseScheme int
22 Compromised bool
23 LockedUntil time.Time
24 PassphraseReset string
25 PassphraseResetCreated time.Time
26 Created time.Time
27 LastSeen time.Time
28 }
30 func (p *Profile) ApplyChange(change ProfileChange) {
31 if change.Name != nil {
32 p.Name = *change.Name
33 }
34 if change.Passphrase != nil {
35 p.Passphrase = *change.Passphrase
36 }
37 if change.Iterations != nil {
38 p.Iterations = *change.Iterations
39 }
40 if change.Salt != nil {
41 p.Salt = *change.Salt
42 }
43 if change.PassphraseScheme != nil {
44 p.PassphraseScheme = *change.PassphraseScheme
45 }
46 if change.Compromised != nil {
47 p.Compromised = *change.Compromised
48 }
49 if change.LockedUntil != nil {
50 p.LockedUntil = *change.LockedUntil
51 }
52 if change.PassphraseReset != nil {
53 p.PassphraseReset = *change.PassphraseReset
54 }
55 if change.PassphraseResetCreated != nil {
56 p.PassphraseResetCreated = *change.PassphraseResetCreated
57 }
58 if change.LastSeen != nil {
59 p.LastSeen = *change.LastSeen
60 }
61 }
63 type ProfileChange struct {
64 Name *string
65 Passphrase *string
66 Iterations *int64
67 Salt *string
68 PassphraseScheme *int
69 Compromised *bool
70 LockedUntil *time.Time
71 PassphraseReset *string
72 PassphraseResetCreated *time.Time
73 LastSeen *time.Time
74 }
76 func (c ProfileChange) Validate() error {
77 // TODO: validate profile changes
78 return nil
79 }
81 type Login struct {
82 Type string
83 Value string
84 ProfileID uuid.ID
85 Created time.Time
86 LastUsed time.Time
87 }
89 type ProfileStore interface {
90 GetProfileByID(id uuid.ID) (Profile, error)
91 GetProfileByLogin(login Login) (Profile, error)
92 SaveProfile(profile Profile) error
93 UpdateProfile(id uuid.ID, change ProfileChange) error
94 DeleteProfile(id uuid.ID) error
95 }
97 func (m *Memstore) GetProfileByID(id uuid.ID) (Profile, error) {
98 m.profileLock.RLock()
99 defer m.profileLock.RUnlock()
100 p, ok := m.profiles[id.String()]
101 if !ok {
102 return Profile{}, ErrProfileNotFound
103 }
104 return p, nil
105 }
107 func (m *Memstore) GetProfileByLogin(login Login) (Profile, error) {
108 // TODO: get profile by login
109 return Profile{}, nil
110 }
112 func (m *Memstore) SaveProfile(profile Profile) error {
113 m.profileLock.Lock()
114 defer m.profileLock.Unlock()
115 _, ok := m.profiles[profile.ID.String()]
116 if ok {
117 return ErrProfileAlreadyExists
118 }
119 m.profiles[profile.ID.String()] = profile
120 return nil
121 }
123 func (m *Memstore) UpdateProfile(id uuid.ID, change ProfileChange) error {
124 m.profileLock.Lock()
125 defer m.profileLock.Unlock()
126 p, ok := m.profiles[id.String()]
127 if !ok {
128 return ErrProfileNotFound
129 }
130 p.ApplyChange(change)
131 m.profiles[id.String()] = p
132 return nil
133 }
135 func (m *Memstore) DeleteProfile(id uuid.ID) error {
136 m.profileLock.Lock()
137 defer m.profileLock.Unlock()
138 _, ok := m.profiles[id.String()]
139 if !ok {
140 return ErrProfileNotFound
141 }
142 delete(m.profiles, id.String())
143 return nil
144 }
146 // TODO: login store
147 // TODO: login delete
148 // TODO: login update