This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
550 additions
and
377 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package auth | ||
|
||
import ( | ||
"github.com/TheThingsNetwork/ttn/api" | ||
"golang.org/x/net/context" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
const tokenKey = "token" | ||
|
||
// TokenCredentials RPC Credentials | ||
type TokenCredentials struct { | ||
token string | ||
tokenFunc func(id string) string | ||
} | ||
|
||
// WithStaticToken injects a static token on each request | ||
func WithStaticToken(token string) *TokenCredentials { | ||
return &TokenCredentials{token: token} | ||
} | ||
|
||
// WithTokenFunc returns TokenCredentials that execute the tokenFunc on each request | ||
func WithTokenFunc(tokenFunc func(id string) string) *TokenCredentials { | ||
return &TokenCredentials{tokenFunc: tokenFunc} | ||
} | ||
|
||
// RequireTransportSecurity implements credentials.PerRPCCredentials | ||
func (c *TokenCredentials) RequireTransportSecurity() bool { return true } | ||
|
||
// GetRequestMetadata implements credentials.PerRPCCredentials | ||
func (c *TokenCredentials) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) { | ||
token, _ := api.TokenFromContext(ctx) | ||
if token != "" { | ||
return map[string]string{tokenKey: token}, nil | ||
} | ||
if c.tokenFunc != nil { | ||
id, _ := api.IDFromContext(ctx) | ||
return map[string]string{tokenKey: c.tokenFunc(id)}, nil | ||
} | ||
if c.token != "" { | ||
return map[string]string{tokenKey: c.token}, nil | ||
} | ||
return map[string]string{tokenKey: ""}, nil | ||
} | ||
|
||
// DialOption returns a DialOption for the TokenCredentials | ||
func (c *TokenCredentials) DialOption() grpc.DialOption { | ||
return grpc.WithPerRPCCredentials(c) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.