FromString re-creates a token from it's string representation (acquired by calling String()). The things I do for pretty tokens.
(s string)
| 63 | // FromString re-creates a token from it's string representation (acquired by calling String()). |
| 64 | // The things I do for pretty tokens. |
| 65 | func FromString(s string) (*Token, error) { |
| 66 | parts := strings.Split(s, "_") |
| 67 | if len(parts) != 3 { |
| 68 | return nil, ErrMalformed |
| 69 | } |
| 70 | |
| 71 | if parts[0] != Prefix { |
| 72 | return nil, ErrMalformed |
| 73 | } |
| 74 | |
| 75 | typ := Type(parts[1]) |
| 76 | if !typ.Validate() { |
| 77 | return nil, ErrMalformed |
| 78 | } |
| 79 | |
| 80 | payload, ok := unmarshalBase62(parts[2]) |
| 81 | if !ok { |
| 82 | return nil, ErrMalformed |
| 83 | } |
| 84 | |
| 85 | if len(payload) > 40 { |
| 86 | return nil, ErrMalformed |
| 87 | } else if len(payload) < 40 { |
| 88 | payload = padLeft(payload, 40) |
| 89 | } |
| 90 | |
| 91 | var id [16]byte |
| 92 | copy(id[:], payload[0:16]) |
| 93 | |
| 94 | var secret [24]byte |
| 95 | copy(secret[:], payload[16:40]) |
| 96 | |
| 97 | return &Token{ |
| 98 | Type: typ, |
| 99 | ID: uuid.UUID(id), |
| 100 | Secret: secret, |
| 101 | }, nil |
| 102 | } |
| 103 | |
| 104 | func FromParts(typ Type, id uuid.UUID, secret []byte) (*Token, error) { |
| 105 | if !typ.Validate() { |