credential.go 812 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package auth
  2. import (
  3. "context"
  4. "google.golang.org/grpc/metadata"
  5. )
  6. type Credential struct {
  7. App string
  8. Token string
  9. }
  10. func (c *Credential) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
  11. return map[string]string{
  12. appKey: c.App,
  13. tokenKey: c.Token,
  14. }, nil
  15. }
  16. func (c *Credential) RequireTransportSecurity() bool {
  17. return false
  18. }
  19. func ParseCredential(ctx context.Context) Credential {
  20. var credential Credential
  21. md, ok := metadata.FromIncomingContext(ctx)
  22. if !ok {
  23. return credential
  24. }
  25. apps, tokens := md[appKey], md[tokenKey]
  26. if len(apps) == 0 || len(tokens) == 0 {
  27. return credential
  28. }
  29. app, token := apps[0], tokens[0]
  30. if len(app) == 0 || len(token) == 0 {
  31. return credential
  32. }
  33. credential.App = app
  34. credential.Token = token
  35. return credential
  36. }