/
api.go
91 lines (78 loc) · 2.46 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package mobileapi
import (
"context"
"encoding/json"
"github.com/cloudtrust/keycloak-bridge/internal/constants"
"github.com/cloudtrust/keycloak-bridge/internal/dto"
"github.com/cloudtrust/keycloak-bridge/internal/keycloakb"
)
// UserInformationRepresentation struct
type UserInformationRepresentation struct {
Accreditations *[]AccreditationRepresentation `json:"accreditations,omitempty"`
Checks *[]CheckRepresentation `json:"checks,omitempty"`
Actions *[]string `json:"actions,omitempty"`
}
// AccreditationRepresentation is a representation of an accreditation
type AccreditationRepresentation struct {
Type *string `json:"type"`
ExpiryDate *string `json:"expiryDate"`
Expired *bool `json:"expired,omitempty"`
}
// CheckRepresentation is a representation of a check
type CheckRepresentation struct {
Type *string `json:"type"`
Nature *string `json:"nature"`
Date *string `json:"date"`
}
// SetAccreditations sets the user accreditations
func (u *UserInformationRepresentation) SetAccreditations(ctx context.Context, attrAccreditations []string, logger keycloakb.Logger) {
if len(attrAccreditations) == 0 {
u.Accreditations = nil
return
}
var accreds []AccreditationRepresentation
for _, accredJSON := range attrAccreditations {
var accred AccreditationRepresentation
if json.Unmarshal([]byte(accredJSON), &accred) == nil {
accred.Expired = keycloakb.IsDateInThePast(accred.ExpiryDate)
accreds = append(accreds, accred)
} else {
logger.Warn(ctx, "msg", "Can't unmarshall JSON", "json", accredJSON)
}
}
u.Accreditations = &accreds
}
// SetChecks sets the user checks
func (u *UserInformationRepresentation) SetChecks(dbChecks []dto.DBCheck) {
if len(dbChecks) == 0 {
u.Checks = nil
return
}
var checks []CheckRepresentation
for _, check := range dbChecks {
var convertedCheck = CheckRepresentation{
Nature: check.Nature,
Type: check.Type,
}
if check.DateTime != nil {
var date = check.DateTime.Format(constants.SupportedDateLayouts[0])
convertedCheck.Date = &date
}
checks = append(checks, convertedCheck)
}
u.Checks = &checks
}
// SetActions sets the user actions
func (u *UserInformationRepresentation) SetActions(availableChecks map[string]bool) {
if len(availableChecks) == 0 {
u.Actions = nil
return
}
var actions []string
for checkType, active := range availableChecks {
if active {
actions = append(actions, checkType)
}
}
u.Actions = &actions
}