-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: protect your website with a password
- Loading branch information
Showing
10 changed files
with
147 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package config | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
// User 登录用户 | ||
type User struct { | ||
Username string | ||
Password string | ||
} | ||
|
||
// ViewFunc func | ||
type ViewFunc func(http.ResponseWriter, *http.Request) | ||
|
||
// BasicAuth basic auth | ||
func BasicAuth(f ViewFunc) ViewFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
// 帐号或密码为空。跳过 | ||
conf, _ := GetConfigCache() | ||
if conf.Username == "" && conf.Password == "" { | ||
// 执行被装饰的函数 | ||
f(w, r) | ||
return | ||
} | ||
|
||
// 认证帐号密码 | ||
basicAuthPrefix := "Basic " | ||
|
||
// 获取 request header | ||
auth := r.Header.Get("Authorization") | ||
// 如果是 http basic auth | ||
if strings.HasPrefix(auth, basicAuthPrefix) { | ||
// 解码认证信息 | ||
payload, err := base64.StdEncoding.DecodeString( | ||
auth[len(basicAuthPrefix):], | ||
) | ||
if err == nil { | ||
pair := bytes.SplitN(payload, []byte(":"), 2) | ||
if len(pair) == 2 && | ||
bytes.Equal(pair[0], []byte(conf.Username)) && | ||
bytes.Equal(pair[1], []byte(conf.Password)) { | ||
// 执行被装饰的函数 | ||
f(w, r) | ||
return | ||
} | ||
} | ||
} | ||
|
||
// 认证失败,提示 401 Unauthorized | ||
// Restricted 可以改成其他的值 | ||
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) | ||
// 401 状态码 | ||
w.WriteHeader(http.StatusUnauthorized) | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters