summaryrefslogtreecommitdiffstats
path: root/modules/base
diff options
context:
space:
mode:
authorUnknwon <joe2010xtmf@163.com>2014-11-07 14:46:13 -0500
committerUnknwon <joe2010xtmf@163.com>2014-11-07 14:46:13 -0500
commitabc57b6e439c5ab9c3b6ed2cedeb10d50f5ae619 (patch)
treef3625f90376d6c4fe2f1ee7a05ba23a5e0b10d9e /modules/base
parenta01b4baca2c1a1d011f19f2255eb9b703658b3dd (diff)
downloadgitea-abc57b6e439c5ab9c3b6ed2cedeb10d50f5ae619.tar.gz
gitea-abc57b6e439c5ab9c3b6ed2cedeb10d50f5ae619.zip
work on #609
Diffstat (limited to 'modules/base')
-rw-r--r--modules/base/tool.go22
1 files changed, 22 insertions, 0 deletions
diff --git a/modules/base/tool.go b/modules/base/tool.go
index 5b56d1f6b7..4d3e1c7bfd 100644
--- a/modules/base/tool.go
+++ b/modules/base/tool.go
@@ -9,7 +9,9 @@ import (
"crypto/md5"
"crypto/rand"
"crypto/sha1"
+ "encoding/base64"
"encoding/hex"
+ "errors"
"fmt"
"hash"
"html/template"
@@ -31,6 +33,26 @@ func EncodeMd5(str string) string {
return hex.EncodeToString(m.Sum(nil))
}
+func BasicAuthDecode(encoded string) (user string, name string, err error) {
+ var s []byte
+ s, err = base64.StdEncoding.DecodeString(encoded)
+ if err != nil {
+ return user, name, err
+ }
+
+ a := strings.Split(string(s), ":")
+ if len(a) == 2 {
+ user, name = a[0], a[1]
+ } else {
+ err = errors.New("decode failed")
+ }
+ return user, name, err
+}
+
+func BasicAuthEncode(username, password string) string {
+ return base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
+}
+
// GetRandomString generate random string by specify chars.
func GetRandomString(n int, alphabets ...byte) string {
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"