Skip to content

Commit b6c8da2

Browse files
committed
WIP PROTI
1 parent 17577f0 commit b6c8da2

8 files changed

Lines changed: 73 additions & 9 deletions

File tree

.vscode/launch.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7+
78
{
89
"name": "Go Run",
910
"type": "go",
1011
"request": "launch",
1112
"mode": "auto",
1213
"program": "main.go"
14+
},
15+
{
16+
"name": "Limit Server Go",
17+
"type": "go",
18+
"request": "launch",
19+
"mode": "auto",
20+
"program": "${workspaceFolder}/servers/limit-server/main.go"
1321
}
1422
]
1523
}

demo.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

proto/limit-server.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ syntax = "proto3";
22

33
package proto;
44

5-
option go_package = "src/limit-server";
5+
option go_package = "servers/limit-server/src";
66

77
enum BuySell {
88
BUY = 0;

servers/limit-server/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import limitserver "github.com/rpsoftech/bullion-server/servers/limit-server/src"
4+
5+
func main() {
6+
limitserver.Start()
7+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package interceptor
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/rpsoftech/bullion-server/src/env"
9+
"google.golang.org/grpc"
10+
"google.golang.org/grpc/codes"
11+
"google.golang.org/grpc/metadata"
12+
"google.golang.org/grpc/status"
13+
)
14+
15+
var (
16+
errMissingMetadata = status.Errorf(codes.InvalidArgument, "missing metadata")
17+
errInvalidToken = status.Errorf(codes.Unauthenticated, "invalid token")
18+
)
19+
20+
// func(ctx context.Context, req any, info *UnaryServerInfo, handler UnaryHandler) (resp any, err error)
21+
22+
func OAuth2Interceptor(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
23+
24+
// return nil, nil
25+
md, ok := metadata.FromIncomingContext(ctx)
26+
if !ok {
27+
return nil, errMissingMetadata
28+
}
29+
if !valid(md[env.RequestTokenHeaderKey]) {
30+
return nil, errInvalidToken
31+
}
32+
m, err := handler(ctx, req)
33+
if err != nil {
34+
logger("RPC failed with error: %v", err)
35+
}
36+
return m, err
37+
}
38+
func logger(format string, a ...any) {
39+
fmt.Printf("LOG:\t"+format+"\n", a...)
40+
}
41+
42+
func valid(authorization []string) bool {
43+
if len(authorization) < 1 {
44+
return false
45+
}
46+
token := strings.TrimPrefix(authorization[0], "Bearer ")
47+
println(authorization)
48+
// Perform the token validation here. For the sake of this example, the code
49+
// here forgoes any of the usual OAuth2 token validation and instead checks
50+
// for a token matching an arbitrary string.
51+
return token == "some-secret-token"
52+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66
"net"
77

8+
interceptor "github.com/rpsoftech/bullion-server/servers/limit-server/src/Interceptor"
89
"google.golang.org/grpc"
910
"google.golang.org/grpc/reflection"
1011
)
@@ -21,14 +22,17 @@ func Start() {
2122
panic(err)
2223
}
2324
srv := grpc.NewServer(
25+
grpc.UnaryInterceptor(interceptor.OAuth2Interceptor),
26+
// grpc.MaxConcurrentStreams(3),
27+
// grpc.
2428
// grpc.UnaryInterceptor()
2529
)
2630
// re
2731
RegisterLimitServerServer(srv, &LimitServerService{})
2832
reflection.Register(srv)
2933

3034
if e := srv.Serve(lis); e != nil {
31-
panic(err)
35+
panic(e)
3236
}
3337
}
3438

File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)