Skip to content

Commit

Permalink
A minimal gRPC credentials implementation for bearer tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmbenton committed Apr 11, 2023
0 parents commit b6dbda4
Show file tree
Hide file tree
Showing 5 changed files with 1,216 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Andrew Benton

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Shared libraries and tools for Go gRPC services

Usage: `go get github.com/riza-io/go-grpc`

#### Credentials

##### Bearer tokens

In your client:
```go
opts := []grpc.DialOption{
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),
grpc.WithPerRPCCredentials(bearer.NewPerRPCCredentials("your bearer token")),
}

conn, err := grpc.Dial(hostname+":443", opts...)
```

Get a token from within your service handler:
```go
token, err := bearer.TokenFromContext(ctx)
```
45 changes: 45 additions & 0 deletions credentials/bearer/bearer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package bearer

import (
"context"
"fmt"

"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
)

const (
mdKey = "authorization"
mdValuePrefix = "Bearer "
mdValueScanFmt = mdValuePrefix + "%s"
)

type Token string

func (t Token) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
ri, _ := credentials.RequestInfoFromContext(ctx)
if err := credentials.CheckSecurityLevel(ri.AuthInfo, credentials.PrivacyAndIntegrity); err != nil {
return nil, fmt.Errorf("unable to transfer bearer.Token PerRPCCredentials: %v", err)
}
return map[string]string{
mdKey: mdValuePrefix + string(t),
}, nil
}

func (t Token) RequireTransportSecurity() bool {
return true
}

func NewPerRPCCredentials(token string) credentials.PerRPCCredentials {
return Token(token)
}

func TokenFromContext(c context.Context) (Token, error) {
var t Token
mv := metadata.ValueFromIncomingContext(c, mdKey)
if mv == nil || len(mv) == 0 {
return t, fmt.Errorf("bearer credential metadata key not found in context")
}
_, err := fmt.Sscanf(mv[0], mdValueScanFmt, &t)
return t, err
}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/riza-io/grpc-go

go 1.17

require google.golang.org/grpc v1.54.0

require (
github.com/golang/protobuf v1.5.2 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
Loading

0 comments on commit b6dbda4

Please sign in to comment.