-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.go
104 lines (89 loc) · 2.2 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package goic
import (
"crypto/elliptic"
"encoding/base64"
"log"
"math/big"
"math/rand"
"net/http"
"strings"
"time"
)
var (
randomPool = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
poolLength = len(randomPool)
urlDecodeRe = strings.NewReplacer("-", "+", "_", "/")
)
// RandomString generates random string of given length
// It sets rand seed on each call and returns generated string.
func RandomString(len int) string {
str := make([]byte, len)
rand.Seed(time.Now().UTC().UnixNano())
for i := range str {
str[i] = randomPool[rand.Intn(poolLength)]
}
return string(str)
}
// Base64UrlDecode decodes JWT segments with base64 accounting for URL chars
func Base64UrlDecode(s string) ([]byte, error) {
pad := len(s) % 4
for pad > 0 {
s += "="
pad--
}
return base64.StdEncoding.DecodeString(urlDecodeRe.Replace(s))
}
// ParseModulo parses the "n" value of jwks key
func ParseModulo(ns string) *big.Int {
buf, _ := Base64UrlDecode(ns)
return new(big.Int).SetBytes(buf)
}
// ParseExponent ParseModulo parses the "e" value of jwks key
func ParseExponent(es string) int {
if es == "AQAB" {
return 65537
}
buf, _ := Base64UrlDecode(es)
return int(new(big.Int).SetBytes(buf).Uint64())
}
// GetCurve gets the elliptic.Curve from last 3 chars of string s
func GetCurve(s string) elliptic.Curve {
s3 := s[len(s)-3:]
if s3 == "256" {
return elliptic.P256()
} else if s3 == "384" {
return elliptic.P384()
} else if s3 == "521" {
return elliptic.P521()
}
return elliptic.P224()
}
// currentURL gets the current request URL with/without query
func currentURL(req *http.Request, query bool) string {
u := req.URL
if u.Scheme == "" {
u.Scheme = "https"
}
if u.Host == "" {
u.Host = req.Header.Get("Host")
if u.Host == "" {
u.Host = req.Host
}
}
if !query {
return strings.Replace(u.String(), "?"+u.RawQuery, "", 1)
}
return u.String()
}
// trapError recovers panics during OpenID operation
func trapError(res http.ResponseWriter) {
msg := "Something went wrong"
if rec := recover(); rec != nil {
switch typ := rec.(type) {
case error:
log.Printf("goic uncaught: %v\n", typ)
msg = typ.Error()
}
http.Error(res, msg, http.StatusInternalServerError)
}
}