Skip to content

Commit 313d046

Browse files
committed
Refactor GetToken
1 parent 8702d3f commit 313d046

File tree

7 files changed

+68
-46
lines changed

7 files changed

+68
-46
lines changed

cmd/plugin-googleid-provider/main.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"flag"
6+
"github.com/grepplabs/kafka-proxy/pkg/apis"
67
"github.com/grepplabs/kafka-proxy/plugin/gateway-client/shared"
78
"github.com/hashicorp/go-plugin"
89
"golang.org/x/oauth2/google"
@@ -18,29 +19,29 @@ type TokenProvider struct {
1819
//TODO: caching, expiry
1920
//TODO: refresh in the half of time
2021
//TODO: send claims
21-
func (p TokenProvider) GetToken(claims []string) (int32, string, error) {
22+
func (p TokenProvider) GetToken(request apis.TokenRequest) (apis.TokenResponse, error) {
2223

2324
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(p.timeout)*time.Second)
2425
defer cancel()
2526

2627
tokenSource, err := google.DefaultTokenSource(ctx, oauth2.UserinfoEmailScope)
2728
if err != nil {
28-
return tokenResponse(1, "")
29+
return tokenResponse(false, 1, "")
2930
}
3031
token, err := tokenSource.Token()
3132
if err != nil {
32-
return tokenResponse(2, "")
33+
return tokenResponse(false, 2, "")
3334
}
3435
if token.Extra("id_token") == nil {
35-
return tokenResponse(3, "")
36+
return tokenResponse(false, 3, "")
3637
}
3738
idToken := token.Extra("id_token").(string)
38-
return tokenResponse(0, idToken)
39+
return tokenResponse(true, 0, idToken)
3940

4041
}
4142

42-
func tokenResponse(status int32, token string) (int32, string, error) {
43-
return status, token, nil
43+
func tokenResponse(success bool, status int32, token string) (apis.TokenResponse, error) {
44+
return apis.TokenResponse{Success: success, Status: status, Token: token}, nil
4445
}
4546

4647
func (f *TokenProvider) flagSet() *flag.FlagSet {

pkg/apis/gateway.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package apis
22

3+
type TokenRequest struct {
4+
Params []string
5+
}
6+
type TokenResponse struct {
7+
Success bool
8+
Status int32
9+
Token string
10+
}
11+
312
type TokenProvider interface {
4-
GetToken(claims []string) (int32, string, error)
13+
GetToken(request TokenRequest) (TokenResponse, error)
514
}

plugin/gateway-client/proto/token-provider.pb.go

Lines changed: 26 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin/gateway-client/proto/token-provider.proto

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ syntax = "proto3";
22
package proto;
33

44
message TokenRequest {
5-
repeated string claims = 1;
5+
repeated string params = 1;
66
}
77

88
message TokenResponse {
9-
int32 status = 1;
10-
string token = 2;
9+
bool success = 1;
10+
int32 status = 2;
11+
string token = 3;
1112
// TODO: expiry ?
1213
}
1314

plugin/gateway-client/shared/grpc.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@ type GRPCClient struct {
1313
client proto.TokenProviderClient
1414
}
1515

16-
func (m *GRPCClient) GetToken(claims []string) (int32, string, error) {
17-
resp, err := m.client.GetToken(context.Background(), &proto.TokenRequest{Claims: claims})
18-
if err != nil {
19-
return 0, "", err
20-
}
21-
return resp.Status, resp.Token, nil
16+
func (m *GRPCClient) GetToken(request apis.TokenRequest) (apis.TokenResponse, error) {
17+
resp, err := m.client.GetToken(context.Background(), &proto.TokenRequest{Params: request.Params})
18+
return apis.TokenResponse{Success: resp.Success, Status: resp.Status, Token: resp.Token}, err
2219
}
2320

2421
// Here is the gRPC server that GRPCClient talks to.
@@ -30,6 +27,6 @@ type GRPCServer struct {
3027
func (m *GRPCServer) GetToken(
3128
ctx context.Context,
3229
req *proto.TokenRequest) (*proto.TokenResponse, error) {
33-
s, t, err := m.Impl.GetToken(req.Claims)
34-
return &proto.TokenResponse{Status: s, Token: t}, err
30+
resp, err := m.Impl.GetToken(apis.TokenRequest{Params: req.Params})
31+
return &proto.TokenResponse{Success: resp.Success, Status: resp.Status, Token: resp.Token}, err
3532
}

plugin/gateway-client/shared/rpc.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,25 @@ import (
77

88
type RPCClient struct{ client *rpc.Client }
99

10-
func (m *RPCClient) GetToken(claims []string) (int32, string, error) {
10+
func (m *RPCClient) GetToken(request apis.TokenRequest) (apis.TokenResponse, error) {
1111
var resp map[string]interface{}
1212
err := m.client.Call("Plugin.GetToken", map[string]interface{}{
13-
"claims": claims,
13+
"params": request.Params,
1414
}, &resp)
15-
return resp["status"].(int32), resp["token"].(string), err
15+
return apis.TokenResponse{Success: resp["success"].(bool), Status: resp["status"].(int32), Token: resp["token"].(string)}, err
1616
}
1717

1818
type RPCServer struct {
1919
Impl apis.TokenProvider
2020
}
2121

2222
func (m *RPCServer) GetToken(args map[string]interface{}, resp *map[string]interface{}) error {
23-
s, t, err := m.Impl.GetToken(args["claims"].([]string))
23+
24+
r, err := m.Impl.GetToken(apis.TokenRequest{Params: args["params"].([]string)})
2425
*resp = map[string]interface{}{
25-
"status": s,
26-
"token": t,
26+
"success": r.Success,
27+
"status": r.Status,
28+
"token": r.Token,
2729
}
2830
return err
2931
}

proxy/auth.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ type AuthClient struct {
2323
//TODO: reset deadlines after method - ok
2424
func (b *AuthClient) sendAndReceiveGatewayAuth(conn DeadlineReaderWriter) error {
2525
//TODO: retrieve from plugin (with timeout)
26-
status, data, err := b.tokenProvider.GetToken([]string{})
26+
resp, err := b.tokenProvider.GetToken(apis.TokenRequest{})
2727
if err != nil {
2828
return err
2929
}
30-
if status != 0 {
31-
return fmt.Errorf("get token failed with status: %d", status)
30+
if !resp.Success {
31+
return fmt.Errorf("get token unsuccessful with status: %d", resp.Status)
3232
}
33-
if data == "" {
34-
return errors.New("received empty token")
33+
if resp.Token == "" {
34+
return errors.New("get token returned empty token")
3535
}
36+
data := resp.Token
37+
logrus.Info(data)
3638

3739
length := len(b.method) + 1 + len(data)
3840
// 8 - bytes magic, 4 bytes length

0 commit comments

Comments
 (0)