-
Notifications
You must be signed in to change notification settings - Fork 0
/
loginTimestamp.go
58 lines (47 loc) · 1.49 KB
/
loginTimestamp.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
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"
)
// GetLastLoginTimestampForIdentityID retrieves last login time for given identity
func (i *Instance) GetLastLoginTimestampForIdentityID(identityID string) (time.Time, error) {
r, err := i.GetLastLoginTimestampsForIdentityIDs([]string{identityID})
if err != nil {
return time.Time{}, err
}
if t, ok := r[identityID]; ok {
return t, nil
}
return time.Time{}, errors.New("no successful login found for ID")
}
// GetLastLoginTimestampsForIdentityIDs retrieves last login times for given identitys
func (i *Instance) GetLastLoginTimestampsForIdentityIDs(identityIDs []string) (map[string]time.Time, error) {
meta, err := i.preflightAuth()
if err != nil {
return map[string]time.Time{}, err
}
ctx := metadata.NewOutgoingContext(context.Background(), meta)
conn, err := grpc.Dial(i.fidentEndpoint, grpc.WithTransportCredentials(credentials.NewTLS(tls.FidentTSLConfig)))
if err != nil {
return map[string]time.Time{}, err
}
defer conn.Close()
c := fidentapi.NewAuthClient(conn)
res, err := c.GetLastLoginTimestamps(ctx, &fidentapi.LoginTimestampRequest{
IdentityId: identityIDs,
})
if err != nil {
return map[string]time.Time{}, err
}
fin := map[string]time.Time{}
for id, unix := range res.Results {
fin[id] = time.Unix(unix, 0)
}
return fin, nil
}