auth.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. "crypto/subtle"
  7. "encoding/base64"
  8. "strconv"
  9. )
  10. const (
  11. AuthUserKey = "user"
  12. )
  13. type (
  14. Accounts map[string]string
  15. authPair struct {
  16. Value string
  17. User string
  18. }
  19. authPairs []authPair
  20. )
  21. func (a authPairs) searchCredential(authValue string) (string, bool) {
  22. if len(authValue) == 0 {
  23. return "", false
  24. }
  25. for _, pair := range a {
  26. if pair.Value == authValue {
  27. return pair.User, true
  28. }
  29. }
  30. return "", false
  31. }
  32. // Implements a basic Basic HTTP Authorization. It takes as arguments a map[string]string where
  33. // the key is the user name and the value is the password, as well as the name of the Realm
  34. // (see http://tools.ietf.org/html/rfc2617#section-1.2)
  35. func BasicAuthForRealm(accounts Accounts, realm string) HandlerFunc {
  36. if realm == "" {
  37. realm = "Authorization Required"
  38. }
  39. realm = "Basic realm=" + strconv.Quote(realm)
  40. pairs := processAccounts(accounts)
  41. return func(c *Context) {
  42. // Search user in the slice of allowed credentials
  43. user, found := pairs.searchCredential(c.Request.Header.Get("Authorization"))
  44. if !found {
  45. // Credentials doesn't match, we return 401 and abort handlers chain.
  46. c.Header("WWW-Authenticate", realm)
  47. c.AbortWithStatus(401)
  48. } else {
  49. // The user credentials was found, set user's id to key AuthUserKey in this context, the userId can be read later using
  50. // c.MustGet(gin.AuthUserKey)
  51. c.Set(AuthUserKey, user)
  52. }
  53. }
  54. }
  55. // Implements a basic Basic HTTP Authorization. It takes as argument a map[string]string where
  56. // the key is the user name and the value is the password.
  57. func BasicAuth(accounts Accounts) HandlerFunc {
  58. return BasicAuthForRealm(accounts, "")
  59. }
  60. func processAccounts(accounts Accounts) authPairs {
  61. if len(accounts) == 0 {
  62. panic("Empty list of authorized credentials")
  63. }
  64. pairs := make(authPairs, 0, len(accounts))
  65. for user, password := range accounts {
  66. if len(user) == 0 {
  67. panic("User can not be empty")
  68. }
  69. value := authorizationHeader(user, password)
  70. pairs = append(pairs, authPair{
  71. Value: value,
  72. User: user,
  73. })
  74. }
  75. return pairs
  76. }
  77. func authorizationHeader(user, password string) string {
  78. base := user + ":" + password
  79. return "Basic " + base64.StdEncoding.EncodeToString([]byte(base))
  80. }
  81. func secureCompare(given, actual string) bool {
  82. if subtle.ConstantTimeEq(int32(len(given)), int32(len(actual))) == 1 {
  83. return subtle.ConstantTimeCompare([]byte(given), []byte(actual)) == 1
  84. } else {
  85. /* Securely compare actual to itself to keep constant time, but always return false */
  86. return subtle.ConstantTimeCompare([]byte(actual), []byte(actual)) == 1 && false
  87. }
  88. }