forked from tetratelabs/protoc-gen-cobra
-
Notifications
You must be signed in to change notification settings - Fork 8
/
init.go
48 lines (39 loc) · 1.09 KB
/
init.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
package jwt
import (
"context"
"fmt"
"github.com/spf13/pflag"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/oauth"
"github.com/NathanBaulch/protoc-gen-cobra/client"
"github.com/NathanBaulch/protoc-gen-cobra/naming"
)
var Config = &config{}
type config struct {
Key string
KeyFile string
}
func init() {
client.RegisterFlagBinder(func(fs *pflag.FlagSet, namer naming.Namer) {
fs.StringVar(&Config.Key, namer("JWT Key"), Config.Key, "JWT key")
fs.StringVar(&Config.KeyFile, namer("JWT KeyFile"), Config.KeyFile, "JWT key file")
})
client.RegisterPreDialer(func(_ context.Context, opts *[]grpc.DialOption) error {
cfg := Config
if cfg.Key != "" {
cred, err := oauth.NewJWTAccessFromKey([]byte(cfg.Key))
if err != nil {
return fmt.Errorf("jwt key: %v", err)
}
*opts = append(*opts, grpc.WithPerRPCCredentials(cred))
}
if cfg.KeyFile != "" {
cred, err := oauth.NewJWTAccessFromFile(cfg.KeyFile)
if err != nil {
return fmt.Errorf("jwt key file: %v", err)
}
*opts = append(*opts, grpc.WithPerRPCCredentials(cred))
}
return nil
})
}