You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/zrpc/internal/auth/credential.go

48 lines
812 B
Go

4 years ago
package auth
import (
"context"
"google.golang.org/grpc/metadata"
)
type Credential struct {
App string
Token string
}
func (c *Credential) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
return map[string]string{
appKey: c.App,
tokenKey: c.Token,
}, nil
}
func (c *Credential) RequireTransportSecurity() bool {
return false
}
func ParseCredential(ctx context.Context) Credential {
var credential Credential
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return credential
}
apps, tokens := md[appKey], md[tokenKey]
if len(apps) == 0 || len(tokens) == 0 {
return credential
}
app, token := apps[0], tokens[0]
if len(app) == 0 || len(token) == 0 {
return credential
}
credential.App = app
credential.Token = token
return credential
}