github.com/cloudwego/hertz@v0.9.3/pkg/app/middlewares/server/basic_auth/basic_auth.go (about) 1 /* 2 * Copyright 2022 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * The MIT License (MIT) 16 * 17 * Copyright (c) 2014 Manuel MartÃnez-Almeida 18 * 19 * Permission is hereby granted, free of charge, to any person obtaining a copy 20 * of this software and associated documentation files (the "Software"), to deal 21 * in the Software without restriction, including without limitation the rights 22 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 23 * copies of the Software, and to permit persons to whom the Software is 24 * furnished to do so, subject to the following conditions: 25 * 26 * The above copyright notice and this permission notice shall be included in 27 * all copies or substantial portions of the Software. 28 * 29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 32 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 33 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 34 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 35 * THE SOFTWARE. 36 * 37 * This file may have been modified by CloudWeGo authors. All CloudWeGo 38 * Modifications are Copyright 2022 CloudWeGo Authors 39 */ 40 41 package basic_auth 42 43 import ( 44 "context" 45 "encoding/base64" 46 "net/http" 47 "strconv" 48 49 "github.com/cloudwego/hertz/internal/bytesconv" 50 "github.com/cloudwego/hertz/pkg/app" 51 ) 52 53 // Accounts is an alias to map[string]string, construct with {"username":"password"} 54 type Accounts map[string]string 55 56 // pairs is an alias to map[string]string, which mean {"header":"username"} 57 type pairs map[string]string 58 59 func (p pairs) findValue(needle string) (v string, ok bool) { 60 v, ok = p[needle] 61 return 62 } 63 64 func constructPairs(accounts Accounts) pairs { 65 length := len(accounts) 66 p := make(pairs, length) 67 for user, password := range accounts { 68 value := "Basic " + base64.StdEncoding.EncodeToString(bytesconv.S2b(user+":"+password)) 69 p[value] = user 70 } 71 return p 72 } 73 74 // BasicAuthForRealm returns a Basic HTTP Authorization middleware. It takes as arguments a map[string]string where 75 // the key is the username and the value is the password, as well as the name of the Realm. 76 // If the realm is empty, "Authorization Required" will be used by default. 77 // (see http://tools.ietf.org/html/rfc2617#section-1.2) 78 func BasicAuthForRealm(accounts Accounts, realm, userKey string) app.HandlerFunc { 79 realm = "Basic realm=" + strconv.Quote(realm) 80 p := constructPairs(accounts) 81 return func(ctx context.Context, c *app.RequestContext) { 82 // Search user in the slice of allowed credentials 83 user, found := p.findValue(c.Request.Header.Get("Authorization")) 84 if !found { 85 // Credentials doesn't match, we return 401 and abort handlers chain. 86 c.Header("WWW-Authenticate", realm) 87 c.AbortWithStatus(http.StatusUnauthorized) 88 return 89 } 90 91 // The user credentials was found, set user's id to key AuthUserKey in this context, the user's id can be read later using 92 c.Set(userKey, user) 93 } 94 } 95 96 // BasicAuth is a constructor of BasicAuth verifier to hertz middleware 97 // It returns a Basic HTTP Authorization middleware. It takes as argument a map[string]string where 98 // the key is the username and the value is the password. 99 func BasicAuth(accounts Accounts) app.HandlerFunc { 100 return BasicAuthForRealm(accounts, "Authorization Required", "user") 101 }