-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
65 lines (50 loc) · 1.51 KB
/
auth.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
package grpcoauth
import (
"context"
"encoding/base64"
"strings"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type AuthFunc func(ctx context.Context, creds *OAuthCredentials) (context.Context, error)
type OAuthCredentials struct {
AccessToken string
ClientID string
ClientSecret string
}
func extractClientIDOrTokenFromContext(ctx context.Context) (*OAuthCredentials, error) {
creds := &OAuthCredentials{}
if token, err := auth.AuthFromMD(ctx, "bearer"); err == nil {
creds.AccessToken = token
}
if val, err := auth.AuthFromMD(ctx, "basic"); err == nil {
b, err := base64.StdEncoding.DecodeString(val)
if err != nil {
return nil, status.Errorf(codes.Unauthenticated, "Basic auth invalid")
}
parts := strings.SplitN(string(b), ":", 2)
if len(parts) != 2 {
return nil, status.Errorf(codes.Unauthenticated, "Basic auth invalid")
}
creds.ClientID = parts[0]
creds.ClientSecret = parts[1]
}
return creds, nil
}
func authFunc(f AuthFunc) auth.AuthFunc {
return func(ctx context.Context) (context.Context, error) {
creds, err := extractClientIDOrTokenFromContext(ctx)
if err != nil {
return nil, err
}
return f(ctx, creds)
}
}
func StreamServerInterceptor(f AuthFunc) grpc.StreamServerInterceptor {
return auth.StreamServerInterceptor(authFunc(f))
}
func UnaryServerInterceptor(f AuthFunc) grpc.UnaryServerInterceptor {
return auth.UnaryServerInterceptor(authFunc(f))
}