auth.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package gin
  5. import (
  6. "encoding/base64"
  7. "net/http"
  8. "strconv"
  9. )
  10. // AuthUserKey is the cookie name for user credential in basic auth.
  11. const AuthUserKey = "user"
  12. // Accounts defines a key/value for user/pass list of authorized logins.
  13. type Accounts map[string]string
  14. type authPair struct {
  15. value string
  16. user string
  17. }
  18. type authPairs []authPair
  19. func (a authPairs) searchCredential(authValue string) (string, bool) {
  20. if authValue == "" {
  21. return "", false
  22. }
  23. for _, pair := range a {
  24. if pair.value == authValue {
  25. return pair.user, true
  26. }
  27. }
  28. return "", false
  29. }
  30. // BasicAuthForRealm returns a Basic HTTP Authorization middleware. It takes as arguments a map[string]string where
  31. // the key is the user name and the value is the password, as well as the name of the Realm.
  32. // If the realm is empty, "Authorization Required" will be used by default.
  33. // (see http://tools.ietf.org/html/rfc2617#section-1.2)
  34. func BasicAuthForRealm(accounts Accounts, realm string) HandlerFunc {
  35. if realm == "" {
  36. realm = "Authorization Required"
  37. }
  38. realm = "Basic realm=" + strconv.Quote(realm)
  39. pairs := processAccounts(accounts)
  40. return func(c *Context) {
  41. // Search user in the slice of allowed credentials
  42. user, found := pairs.searchCredential(c.requestHeader("Authorization"))
  43. if !found {
  44. // Credentials doesn't match, we return 401 and abort handlers chain.
  45. c.Header("WWW-Authenticate", realm)
  46. c.AbortWithStatus(http.StatusUnauthorized)
  47. return
  48. }
  49. // The user credentials was found, set user's id to key AuthUserKey in this context, the user's id can be read later using
  50. // c.MustGet(gin.AuthUserKey).
  51. c.Set(AuthUserKey, user)
  52. }
  53. }
  54. // BasicAuth returns a Basic HTTP Authorization middleware. It takes as argument a map[string]string where
  55. // the key is the user name and the value is the password.
  56. func BasicAuth(accounts Accounts) HandlerFunc {
  57. return BasicAuthForRealm(accounts, "")
  58. }
  59. func processAccounts(accounts Accounts) authPairs {
  60. assert1(len(accounts) > 0, "Empty list of authorized credentials")
  61. pairs := make(authPairs, 0, len(accounts))
  62. for user, password := range accounts {
  63. assert1(user != "", "User can not be empty")
  64. value := authorizationHeader(user, password)
  65. pairs = append(pairs, authPair{
  66. value: value,
  67. user: user,
  68. })
  69. }
  70. return pairs
  71. }
  72. func authorizationHeader(user, password string) string {
  73. base := user + ":" + password
  74. return "Basic " + base64.StdEncoding.EncodeToString([]byte(base))
  75. }