-
Notifications
You must be signed in to change notification settings - Fork 0
/
accountDetails.go
92 lines (76 loc) · 2.31 KB
/
accountDetails.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
92
package manage
import (
"context"
"errors"
"time"
"github.com/fident/go-manage/fidentapi"
"github.com/fident/go-manage/tls"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
)
// AccountDetail item structure
type AccountDetail struct {
IdentityID string
Username string
Attributes map[string]string
Created time.Time
}
// GetFirstNameAttribute returns the first name from account detail attributes
func (a *AccountDetail) GetFirstNameAttribute() string {
return a.Attributes[attributeKeyFirstName]
}
// GetLastNameAttribute returns the last name from account detail attributes
func (a *AccountDetail) GetLastNameAttribute() string {
return a.Attributes[attributeKeyLastName]
}
// GetEmailAddress returns email address for account
func (a *AccountDetail) GetEmailAddress() string {
return a.Username
}
// GetAccountDetailsForIdentityID retrieves account details for given identity id
func (i *Instance) GetAccountDetailsForIdentityID(identityID string) (AccountDetail, error) {
r, err := i.GetAccountDetailsForIdentityIDs([]string{identityID})
if err != nil {
return AccountDetail{}, err
}
if len(r) > 0 {
return r[0], nil
}
return AccountDetail{}, errors.New("no details found for ID")
}
// GetAccountDetailsForIdentityIDs retrieves account details for given identity ids
func (i *Instance) GetAccountDetailsForIdentityIDs(identityIDs []string) ([]AccountDetail, error) {
meta, err := i.preflightAuth()
if err != nil {
return []AccountDetail{}, err
}
ctx := metadata.NewOutgoingContext(context.Background(), meta)
conn, err := grpc.Dial(i.fidentEndpoint, grpc.WithTransportCredentials(credentials.NewTLS(tls.FidentTSLConfig)))
if err != nil {
return []AccountDetail{}, err
}
defer conn.Close()
c := fidentapi.NewAuthClient(conn)
res, err := c.GetAccountDetails(ctx, &fidentapi.AccountDetailRequest{
IdentityId: identityIDs,
})
if err != nil {
return []AccountDetail{}, err
}
fin := []AccountDetail{}
for _, det := range res.Results {
A := AccountDetail{
IdentityID: det.GetIdentityId(),
Username: det.GetUsername(),
Created: time.Unix(det.GetCreated(), 0),
}
at := map[string]string{}
for _, ar := range det.Attributes {
at[ar.GetKey()] = ar.GetValue()
}
A.Attributes = at
fin = append(fin, A)
}
return fin, nil
}