1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package gin67import (8 "crypto/subtle"9 "encoding/base64"10 "net/http"11 "strconv"1213 "github.com/gin-gonic/gin/internal/bytesconv"14)1516// AuthUserKey is the cookie name for user credential in basic auth.17const AuthUserKey = "user"1819// AuthProxyUserKey is the cookie name for proxy_user credential in basic auth for proxy.20const AuthProxyUserKey = "proxy_user"2122// Accounts defines a key/value for user/pass list of authorized logins.23type Accounts map[string]string2425type authPair struct {26 value string27 user string28}2930type authPairs []authPair3132func (a authPairs) searchCredential(authValue string) (string, bool) {33 if authValue == "" {34 return "", false35 }36 for _, pair := range a {37 if subtle.ConstantTimeCompare(bytesconv.StringToBytes(pair.value), bytesconv.StringToBytes(authValue)) == 1 {38 return pair.user, true39 }40 }41 return "", false42}4344// BasicAuthForRealm returns a Basic HTTP Authorization middleware. It takes as arguments a map[string]string where45// the key is the user name and the value is the password, as well as the name of the Realm.46// If the realm is empty, "Authorization Required" will be used by default.47// (see http://tools.ietf.org/html/rfc2617#section-1.2)48func BasicAuthForRealm(accounts Accounts, realm string) HandlerFunc {49 if realm == "" {50 realm = "Authorization Required"51 }52 realm = "Basic realm=" + strconv.Quote(realm)53 pairs := processAccounts(accounts)54 return func(c *Context) {55 // Search user in the slice of allowed credentials56 user, found := pairs.searchCredential(c.requestHeader("Authorization"))57 if !found {58 // Credentials doesn't match, we return 401 and abort handlers chain.59 c.Header("WWW-Authenticate", realm)60 c.AbortWithStatus(http.StatusUnauthorized)61 return62 }6364 // The user credentials was found, set user's id to key AuthUserKey in this context, the user's id can be read later using65 // c.MustGet(gin.AuthUserKey).66 c.Set(AuthUserKey, user)67 }68}6970// BasicAuth returns a Basic HTTP Authorization middleware. It takes as argument a map[string]string where71// the key is the user name and the value is the password.72func BasicAuth(accounts Accounts) HandlerFunc {73 return BasicAuthForRealm(accounts, "")74}7576func processAccounts(accounts Accounts) authPairs {77 length := len(accounts)78 assert1(length > 0, "Empty list of authorized credentials")79 pairs := make(authPairs, 0, length)80 for user, password := range accounts {81 assert1(user != "", "User can not be empty")82 value := authorizationHeader(user, password)83 pairs = append(pairs, authPair{84 value: value,85 user: user,86 })87 }88 return pairs89}9091func authorizationHeader(user, password string) string {92 base := user + ":" + password93 return "Basic " + base64.StdEncoding.EncodeToString(bytesconv.StringToBytes(base))94}9596// BasicAuthForProxy returns a Basic HTTP Proxy-Authorization middleware.97// If the realm is empty, "Proxy Authorization Required" will be used by default.98func BasicAuthForProxy(accounts Accounts, realm string) HandlerFunc {99 if realm == "" {100 realm = "Proxy Authorization Required"101 }102 realm = "Basic realm=" + strconv.Quote(realm)103 pairs := processAccounts(accounts)104 return func(c *Context) {105 proxyUser, found := pairs.searchCredential(c.requestHeader("Proxy-Authorization"))106 if !found {107 // Credentials doesn't match, we return 407 and abort handlers chain.108 c.Header("Proxy-Authenticate", realm)109 c.AbortWithStatus(http.StatusProxyAuthRequired)110 return111 }112 // The proxy_user credentials was found, set proxy_user's id to key AuthProxyUserKey in this context, the proxy_user's id can be read later using113 // c.MustGet(gin.AuthProxyUserKey).114 c.Set(AuthProxyUserKey, proxyUser)115 }116}