https://github.com/bwplotka/go-tokenauth
Bunch of useful token based auth sources!
https://github.com/bwplotka/go-tokenauth
Last synced: 10 months ago
JSON representation
Bunch of useful token based auth sources!
- Host: GitHub
- URL: https://github.com/bwplotka/go-tokenauth
- Owner: bwplotka
- License: apache-2.0
- Created: 2017-08-16T15:07:07.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-08-17T12:01:38.000Z (over 8 years ago)
- Last Synced: 2024-10-30T04:28:34.431Z (over 1 year ago)
- Language: Go
- Homepage:
- Size: 13.7 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-tokenauth
[](https://travis-ci.org/Bplotka/go-tokenauth) [](https://goreportcard.com/report/github.com/Bplotka/go-tokenauth)
This package contains different useful Auth Sources that can return valid auth token for future use in a form of string.
It also contains tiny interface called `tokenauth.Source` that allows to create more generic (e.g HTTP, gRPC) Clients:
```go
type Source interface {
// Name of the auth source.
Name() string
// Token allows the source to return a valid token for specific authorization type in a form of string.
//
// Example usage:
// - filling Authorization HTTP header with valid auth.
// In that case it is up to caller to properly save it into specific http request header (usually called "Authorization")
// and add "bearer" prefix if needed.
Token(context.Context) (string, error)
}
```
## Example usage:
The usefulness of this interface can be shown in this example, when we wrap http.RoundTripper to inject required auth:
```go
package example
import (
"fmt"
"net/http"
"github.com/Bplotka/go-tokenauth"
"github.com/Bplotka/go-tokenauth/direct"
)
func newExampleTripper(parent http.RoundTripper /* , */) http.RoundTripper {
auth := directauth.New("direct", "token1")
// or:
// oidcauth.New(...)
// oauth2auth.New(...)
// k8sauth.New(...)
return &exampleTripper{
parent: parent,
auth: auth,
}
}
type exampleTripper struct {
parent http.RoundTripper
auth tokenauth.Source
}
func (t *exampleTripper) RoundTrip(req *http.Request) (*http.Response, error) {
token, err := t.auth.Token(req.Context())
if err != nil {
// handle err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
return t.parent.RoundTrip(req)
}
```
See ready to use [tripper](./http/tripper.go)