/
batch_authorize.go
51 lines (40 loc) · 1.05 KB
/
batch_authorize.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
package authz
import (
"context"
"fmt"
"connectrpc.com/connect"
authzv1alpha1 "github.com/common-fate/sdk/gen/commonfate/authz/v1alpha1"
)
type DecisionNotFoundError struct {
Key string
}
func (e DecisionNotFoundError) Error() string {
return fmt.Sprintf("decision not found for client_id: %s", e.Key)
}
type BatchAuthorizeInput struct {
Requests map[string]Request
}
type BatchAuthorizeResponse struct {
// Evaluations made by the authz service.
Evaluations []*authzv1alpha1.Evaluation
}
func (c *Client) BatchAuthorize(ctx context.Context, input BatchAuthorizeInput) (BatchAuthorizeResponse, error) {
r := make([]*authzv1alpha1.Request, 0)
for _, item := range input.Requests {
req, err := item.ToAPI()
if err != nil {
return BatchAuthorizeResponse{}, err
}
r = append(r, req)
}
res, err := c.raw.BatchAuthorize(ctx, connect.NewRequest(&authzv1alpha1.BatchAuthorizeRequest{
Requests: r,
}))
if err != nil {
return BatchAuthorizeResponse{}, err
}
ret := BatchAuthorizeResponse{
Evaluations: res.Msg.Evaluations,
}
return ret, nil
}