/
auth.go
56 lines (44 loc) · 1.12 KB
/
auth.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
package web
import (
"crypto/rsa"
"fmt"
"strings"
"github.com/dgrijalva/jwt-go"
)
type Claims struct {
Username string `json:"username"`
jwt.StandardClaims
}
func getJwtUsername(tknStr string, JwtKey *rsa.PublicKey) (string, error) {
claims := &Claims{}
tkn, err := jwt.ParseWithClaims(tknStr, claims, func(token *jwt.Token) (interface{}, error) {
return JwtKey, nil
})
if err != nil {
return "", err
}
if !tkn.Valid {
return "", fmt.Errorf("JWT Invalid")
}
return claims.Username, nil
}
func getJwtServiceName(tknStr string, JwtKey *rsa.PublicKey) (string, error) {
username, err := getJwtUsername(tknStr, JwtKey)
if err != nil {
return "", err
}
if !strings.HasSuffix(username, "@services.geegle.org") {
return "", fmt.Errorf("Not a service account")
}
return username[:len(username)-20], nil
}
func getJwtLDAPName(tknStr string, JwtKey *rsa.PublicKey) (string, error) {
username, err := getJwtUsername(tknStr, JwtKey)
if err != nil {
return "", err
}
if !strings.HasSuffix(username, "@geegle.org") {
return "", fmt.Errorf("Not a corp account")
}
return username[:len(username)-11], nil
}