-
Notifications
You must be signed in to change notification settings - Fork 3
/
functions.go
85 lines (76 loc) · 2.71 KB
/
functions.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
package iam
import (
"context"
"encoding/json"
"errors"
"time"
"github.com/SKF/proto/common"
"github.com/SKF/go-eventsource/eventsource"
iam_grpcapi "github.com/SKF/proto/iam"
)
func (c *client) CheckAuthentication(token, arn string) (claims iam_grpcapi.UserClaims, err error) {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
return c.CheckAuthenticationWithContext(ctx, token, arn)
}
func (c *client) CheckAuthenticationWithContext(ctx context.Context, token, arn string) (claims iam_grpcapi.UserClaims, err error) {
input := &iam_grpcapi.CheckAuthenticationInput{Token: token, MethodArn: arn}
output, err := c.api.CheckAuthentication(ctx, input)
if output != nil {
claims = *output
} else if err == nil {
err = errors.New("no output")
}
return
}
func (c *client) CheckAuthenticationByEndpoint(token, api, method, endpoint string) (claims iam_grpcapi.UserClaims, err error) {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
return c.CheckAuthenticationByEndpointWithContext(ctx, token, api, method, endpoint)
}
func (c *client) CheckAuthenticationByEndpointWithContext(ctx context.Context, token, api, method, endpoint string) (claims iam_grpcapi.UserClaims, err error) {
input := &iam_grpcapi.CheckAuthenticationByEndpointInput{
Api: api,
Token: token,
Method: method,
Endpoint: endpoint,
}
output, err := c.api.CheckAuthenticationByEndpoint(ctx, input)
if output != nil {
claims = *output
} else if err == nil {
err = errors.New("no output")
}
return
}
func (c *client) GetNodesByUser(userID string) (nodeIDs []string, err error) {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
return c.GetNodesByUserWithContext(ctx, userID)
}
func (c *client) GetNodesByUserWithContext(ctx context.Context, userID string) (nodeIDs []string, err error) {
input := &iam_grpcapi.GetHierarchyRelationsInput{UserId: userID}
output, err := c.api.GetHierarchyRelations(ctx, input)
if err != nil {
return
}
nodeIDs = output.NodeIds
return
}
func (c *client) GetEventRecords(since int, limit *int32) (records []eventsource.Record, err error) {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
return c.GetEventRecordsWithContext(ctx, since, limit)
}
func (c *client) GetEventRecordsWithContext(ctx context.Context, since int, limit *int32) (records []eventsource.Record, err error) {
input := iam_grpcapi.GetEventRecordsInput{Since: int64(since)}
if limit != nil {
input.Limit = &common.PrimitiveInt32{Value: *limit}
}
output, err := c.api.GetEventRecords(ctx, &input)
if err != nil {
return
}
err = json.Unmarshal(output.Records, &records)
return
}