1 package database
2
3 import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "strings"
8
9 "edge-infra.dev/pkg/edge/iam/config"
10 "edge-infra.dev/pkg/edge/iam/profile"
11 "edge-infra.dev/pkg/edge/iam/util"
12
13 "github.com/pkg/errors"
14
15 "time"
16 )
17
18 func (s *Store) CreateAlias(ctx context.Context, alias, subject string) (err error) {
19 key := keyFrom(KeyPrefixAlias, alias)
20 temp := make(map[string]string)
21 temp["subject"] = strings.ToLower(subject)
22 payload, err := json.Marshal(temp)
23 if err != nil {
24 return errors.WithStack(err)
25 }
26 if err := s.createDoc(ctx, key, payload); err != nil {
27 return errors.Wrap(err, "failed to create alias")
28 }
29 return nil
30 }
31 func (s *Store) GetSubjectFromAlias(ctx context.Context, alias string) (subject string, err error) {
32 key := keyFrom(KeyPrefixAlias, alias)
33 var doc *Doc
34 if doc, err = s.getDoc(ctx, key); err != nil {
35 return "", errors.WithMessage(err, "failed to retrieve subject")
36 }
37 if doc == nil {
38 return "", errors.New("alias not found")
39 }
40
41 data := doc.Value
42 var value map[string]string
43 err = json.Unmarshal(data, &value)
44 if err != nil {
45 return "", errors.WithMessage(err, "schema not valid")
46 }
47 return strings.ToLower(value["subject"]), nil
48 }
49 func (s *Store) CreateIdentityProfile(ctx context.Context, p profile.Profile) error {
50 key := keyFrom(KeyPrefixProfile, p.Subject)
51 p.LastUpdated = time.Now().Unix()
52
53 payload, err := json.Marshal(p)
54 if err != nil {
55 return errors.WithStack(err)
56 }
57
58 if err := s.updateDoc(ctx, key, payload, WithExpiration(config.GetProfileTTL())); err != nil {
59 return errors.Wrap(err, "failed to save profile")
60 }
61
62 return nil
63 }
64
65 func (s *Store) GetIdentityProfile(ctx context.Context, subject string) (profile *profile.Profile, err error) {
66 key := keyFrom(KeyPrefixProfile, subject)
67 var doc *Doc
68 if doc, err = s.getDoc(ctx, key); err != nil {
69 return nil, errors.WithMessage(err, "failed to retrieve profile")
70 }
71 if doc == nil {
72 return nil, nil
73 }
74
75 data := doc.Value
76 err = json.Unmarshal(data, &profile)
77 if err != nil {
78 return nil, errors.WithMessage(err, "profile schema not valid")
79 }
80
81 return profile, nil
82 }
83
84 func (s *Store) ExpireIdentityProfile(ctx context.Context, subject string) (err error) {
85 profile, err := s.GetIdentityProfile(ctx, subject)
86 if err != nil {
87 return errors.WithStack(err)
88 }
89 if profile == nil {
90 return fmt.Errorf("no such profile")
91 }
92
93
94 lastUpdated := time.Unix(profile.LastUpdated, 0)
95 dayBefore := lastUpdated.AddDate(0, 0, -1)
96 profile.LastUpdated = dayBefore.Unix()
97
98 marshalledProfile, err := json.Marshal(profile)
99 if err != nil {
100 return errors.WithStack(err)
101 }
102
103
104 key := keyFrom(KeyPrefixProfile, subject)
105 if err := s.updateDoc(ctx, key, marshalledProfile); err != nil {
106 return errors.Wrap(err, "failed to save profile")
107 }
108
109 return nil
110 }
111 func (s *Store) AddAliasToProfile(ctx context.Context, userProfile *profile.Profile) error {
112 alias, _ := util.RandomStringGenerator(8)
113 err := s.CreateAlias(ctx, alias, userProfile.Subject)
114 if err != nil {
115 return errors.Wrap(err, "failed to create alias")
116 }
117 userProfile.Alias = alias
118 err = s.CreateIdentityProfile(ctx, *userProfile)
119 if err != nil {
120 return errors.Wrap(err, "failed to update profile with alias")
121 }
122 return nil
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
View as plain text