jwttoken.go 1.8 KB

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