-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alexander Yastrebov <[email protected]>
- Loading branch information
1 parent
4dcbf4b
commit c997fd2
Showing
5 changed files
with
128 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package jwt | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"errors" | ||
"strings" | ||
) | ||
|
||
var ( | ||
errInvalidToken = errors.New("invalid jwt token") | ||
) | ||
|
||
type Token struct { | ||
Claims map[string]interface{} | ||
} | ||
|
||
func Parse(value string) (*Token, error) { | ||
parts := strings.Split(value, ".") | ||
if len(parts) != 3 { | ||
return nil, errInvalidToken | ||
} | ||
|
||
var token Token | ||
err := unmarshalBase64JSON(parts[1], &token.Claims) | ||
if err != nil { | ||
return nil, errInvalidToken | ||
} | ||
|
||
return &token, nil | ||
} | ||
|
||
func unmarshalBase64JSON(s string, v interface{}) error { | ||
d, err := base64.RawURLEncoding.DecodeString(s) | ||
if err != nil { | ||
return err | ||
} | ||
return json.Unmarshal(d, v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package jwt | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestParse(t *testing.T) { | ||
for _, tt := range []struct { | ||
value string | ||
ok bool | ||
claims map[string]interface{} | ||
}{ | ||
{ | ||
value: "", | ||
ok: false, | ||
}, { | ||
value: "x", | ||
ok: false, | ||
}, { | ||
value: "x.y", | ||
ok: false, | ||
}, { | ||
value: "x.y.z", | ||
ok: false, | ||
}, { | ||
value: "..", | ||
ok: false, | ||
}, { | ||
value: "x..z", | ||
ok: false, | ||
}, { | ||
value: "x." + marshalBase64JSON(t, map[string]interface{}{"hello": "world"}) + ".z", | ||
ok: true, | ||
claims: map[string]interface{}{"hello": "world"}, | ||
}, { | ||
value: "." + marshalBase64JSON(t, map[string]interface{}{"no header": "no signature"}) + ".", | ||
ok: true, | ||
claims: map[string]interface{}{"no header": "no signature"}, | ||
}, | ||
} { | ||
token, err := Parse(tt.value) | ||
if tt.ok { | ||
if err != nil { | ||
t.Errorf("unexpected error for %s: %v", tt.value, err) | ||
continue | ||
} | ||
} else { | ||
continue | ||
} | ||
|
||
if !reflect.DeepEqual(tt.claims, token.Claims) { | ||
t.Errorf("claims mismatch, expected: %v, got %v", tt.claims, token.Claims) | ||
} | ||
} | ||
} | ||
|
||
func marshalBase64JSON(t *testing.T, v interface{}) string { | ||
d, err := json.Marshal(v) | ||
if err != nil { | ||
t.Fatalf("failed to marshal json: %v, %v", v, err) | ||
} | ||
return base64.RawURLEncoding.EncodeToString(d) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters