auth.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package auth
  2. import (
  3. "context"
  4. "time"
  5. "git.i2edu.net/i2/go-zero/core/collection"
  6. "git.i2edu.net/i2/go-zero/core/stores/redis"
  7. "google.golang.org/grpc/codes"
  8. "google.golang.org/grpc/metadata"
  9. "google.golang.org/grpc/status"
  10. )
  11. const defaultExpiration = 5 * time.Minute
  12. // An Authenticator is used to authenticate the rpc requests.
  13. type Authenticator struct {
  14. store *redis.Redis
  15. key string
  16. cache *collection.Cache
  17. strict bool
  18. }
  19. // NewAuthenticator returns an Authenticator.
  20. func NewAuthenticator(store *redis.Redis, key string, strict bool) (*Authenticator, error) {
  21. cache, err := collection.NewCache(defaultExpiration)
  22. if err != nil {
  23. return nil, err
  24. }
  25. return &Authenticator{
  26. store: store,
  27. key: key,
  28. cache: cache,
  29. strict: strict,
  30. }, nil
  31. }
  32. // Authenticate authenticates the given ctx.
  33. func (a *Authenticator) Authenticate(ctx context.Context) error {
  34. md, ok := metadata.FromIncomingContext(ctx)
  35. if !ok {
  36. return status.Error(codes.Unauthenticated, missingMetadata)
  37. }
  38. apps, tokens := md[appKey], md[tokenKey]
  39. if len(apps) == 0 || len(tokens) == 0 {
  40. return status.Error(codes.Unauthenticated, missingMetadata)
  41. }
  42. app, token := apps[0], tokens[0]
  43. if len(app) == 0 || len(token) == 0 {
  44. return status.Error(codes.Unauthenticated, missingMetadata)
  45. }
  46. return a.validate(app, token)
  47. }
  48. func (a *Authenticator) validate(app, token string) error {
  49. expect, err := a.cache.Take(app, func() (interface{}, error) {
  50. return a.store.Hget(a.key, app)
  51. })
  52. if err != nil {
  53. if a.strict {
  54. return status.Error(codes.Internal, err.Error())
  55. }
  56. return nil
  57. }
  58. if token != expect {
  59. return status.Error(codes.Unauthenticated, accessDenied)
  60. }
  61. return nil
  62. }