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

5
package gin
6

7
import (
8
	"encoding/base64"
9
	"net/http"
10
	"strconv"
11

12
	"github.com/gin-gonic/gin/internal/bytesconv"
13
)
14

15
// AuthUserKey is the cookie name for user credential in basic auth.
16
const AuthUserKey = "user"
17

18
// Accounts defines a key/value for user/pass list of authorized logins.
19
type Accounts map[string]string
20

21
type authPair struct {
22
	value string
23
	user  string
24
}
25

26
type authPairs []authPair
27

28
func (a authPairs) searchCredential(authValue string) (string, bool) {
29 14
	if authValue == "" {
30 14
		return "", false
31
	}
32
	for _, pair := range a {
33 14
		if pair.value == authValue {
34 14
			return pair.user, true
35
		}
36
	}
37 14
	return "", false
38
}
39

40
// BasicAuthForRealm returns a Basic HTTP Authorization middleware. It takes as arguments a map[string]string where
41
// the key is the user name and the value is the password, as well as the name of the Realm.
42
// If the realm is empty, "Authorization Required" will be used by default.
43
// (see http://tools.ietf.org/html/rfc2617#section-1.2)
44
func BasicAuthForRealm(accounts Accounts, realm string) HandlerFunc {
45 14
	if realm == "" {
46 14
		realm = "Authorization Required"
47
	}
48 14
	realm = "Basic realm=" + strconv.Quote(realm)
49 14
	pairs := processAccounts(accounts)
50 14
	return func(c *Context) {
51
		// Search user in the slice of allowed credentials
52 14
		user, found := pairs.searchCredential(c.requestHeader("Authorization"))
53 14
		if !found {
54
			// Credentials doesn't match, we return 401 and abort handlers chain.
55 14
			c.Header("WWW-Authenticate", realm)
56 14
			c.AbortWithStatus(http.StatusUnauthorized)
57 14
			return
58
		}
59

60
		// The user credentials was found, set user's id to key AuthUserKey in this context, the user's id can be read later using
61
		// c.MustGet(gin.AuthUserKey).
62 14
		c.Set(AuthUserKey, user)
63
	}
64
}
65

66
// BasicAuth returns a Basic HTTP Authorization middleware. It takes as argument a map[string]string where
67
// the key is the user name and the value is the password.
68
func BasicAuth(accounts Accounts) HandlerFunc {
69 14
	return BasicAuthForRealm(accounts, "")
70
}
71

72
func processAccounts(accounts Accounts) authPairs {
73 14
	length := len(accounts)
74 14
	assert1(length > 0, "Empty list of authorized credentials")
75 14
	pairs := make(authPairs, 0, length)
76
	for user, password := range accounts {
77 14
		assert1(user != "", "User can not be empty")
78 14
		value := authorizationHeader(user, password)
79 14
		pairs = append(pairs, authPair{
80 14
			value: value,
81 14
			user:  user,
82 14
		})
83
	}
84 14
	return pairs
85
}
86

87
func authorizationHeader(user, password string) string {
88 14
	base := user + ":" + password
89 14
	return "Basic " + base64.StdEncoding.EncodeToString(bytesconv.StringToBytes(base))
90
}

Read our documentation on viewing source code .

Loading