Core functionality for new aiterp.net servers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
853 B

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package auth
  2. import (
  3. "errors"
  4. "strings"
  5. )
  6. var methods []Authenticator
  7. // Register a method
  8. func Register(method Authenticator) {
  9. methods = append(methods, method)
  10. }
  11. // FindAuthenticator finds the first Method that answers with
  12. // the ID().
  13. func FindAuthenticator(id string) Authenticator {
  14. for _, method := range methods {
  15. if method.ID() == id {
  16. return method
  17. }
  18. }
  19. return nil
  20. }
  21. // ListAuthenticators gets a copy of the method list
  22. func ListAuthenticators() []Authenticator {
  23. dst := make([]Authenticator, len(methods))
  24. copy(dst, methods)
  25. return dst
  26. }
  27. func FindUser(fullid string) (*User, error) {
  28. split := strings.SplitN(fullid, ":", 2)
  29. autherID := split[0]
  30. userID := split[1]
  31. auther := FindAuthenticator(autherID)
  32. if auther == nil {
  33. return nil, errors.New("auth: auther not found")
  34. }
  35. return auther.Find(userID), nil
  36. }