Debian Codegraph @

photon/lightwave/jwttoken.go

raw
1 package lightwave
2
3 import (
4 "encoding/base64"
5 "encoding/json"
6 "strings"
7 )
8
9 type JWTToken struct {
10 TokenId string `json:"jti"`
11 Algorithm string `json:"alg"`
12 Subject string `json:"sub"`
13 Audience []string `json:"aud"`
14 Groups []string `json:"groups"`
15 Issuer string `json:"iss"`
16 IssuedAt int64 `json:"iat"`
17 Expires int64 `json:"exp"`
18 Scope string `json:"scope"`
19 TokenType string `json:"token_type"`
20 TokenClass string `json:"token_class"`
21 Tenant string `json:"tenant"`
22 // It's possible to have more fields depending on how Lightwave defines the token.
23 // This covers all the fields we currently have.
24 }
25
26 // A JSON web token is a set of Base64 encoded strings separated by a period (.)
27 // When decoded, it will either be JSON text or a signature
28 // Here we decode the strings into a single token structure. We do not parse the signature.
29 func ParseTokenDetails(token string) (jwtToken *JWTToken) {
30 jwtToken = &JWTToken{}
31
32 chunks := strings.Split(token, ".")
33 for _, chunk := range chunks {
34 json_string, err := base64.RawURLEncoding.DecodeString(chunk)
35 if err == nil {
36 // Ignore errors. We expect that the signature is not JSON,
37 // so unmarshalling it will fail. That's fine. We'll extract
38 // all the data we can.
39 _ = json.Unmarshal(json_string, &jwtToken)
40 }
41 }
42
43 return jwtToken
44 }
45
46 // A JSON web token is a set of Base64 encoded strings separated by a period (.)
47 // When decoded, it will either be JSON text or a signature
48 // Here we parse the full JSON text. We do not parse the signature.
49 func ParseRawTokenDetails(token string) (jwtToken []string, err error) {
50 chunks := strings.Split(token, ".")
51 for _, chunk := range chunks {
52 jsonString, err := base64.RawURLEncoding.DecodeString(chunk)
53 if err == nil {
54 jwtToken = append(jwtToken, string(jsonString))
55 }
56 }
57
58 return jwtToken, err
59 }