/
validator.go
56 lines (46 loc) · 1.08 KB
/
validator.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
package main
import (
"context"
"fmt"
"log"
"os"
pb "github.com/graphql-services/oauth/grpc"
opentracing "github.com/opentracing/opentracing-go"
"google.golang.org/grpc"
)
var client *pb.ScopeValidatorClient
func getClient() (*pb.ScopeValidatorClient, error) {
if client == nil {
URL := os.Getenv("SCOPE_VALIDATOR_URL")
if URL != "" {
conn, err := grpc.Dial(URL, grpc.WithInsecure())
if err != nil {
return nil, err
}
log.Printf("Created scope validator client with URL: %s", URL)
c := pb.NewScopeValidatorClient(conn)
client = &c
}
}
return client, nil
}
func validateScopeForUser(ctx context.Context, scope, userID string) (string, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "validateScopeForUser")
defer span.Finish()
c, err := getClient()
if err != nil {
return "", err
}
if c != nil {
req := &pb.ValidateRequest{UserID: userID, Scopes: scope}
res, err := (*c).Validate(ctx, req)
if err != nil {
return "", err
}
if !res.Valid {
return "", fmt.Errorf("invalid scopes")
}
scope = res.Scopes
}
return scope, nil
}