This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
forked from mdigger/jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'mdigger/master'
- Loading branch information
Showing
6 changed files
with
112 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// jwt поддерживает работу с токенами в формате JSON Web Token. | ||
// | ||
// В данной библиотеке я решил ограничиться только поддержкой основного метода подписи — HS256. | ||
// Во-первых, потому что он единственный является обязательным. Во-вторых, совершенно не хотелось | ||
// подключать дополнительные библиотеки без необходимости. В-третьих, с ним получается одна из | ||
// самых коротких подписей, что, как вы понимаете, в прямую касается длины получаемого токена. | ||
// В общем, мне показалось этого достаточно. | ||
package jwt |
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,35 @@ | ||
package jwt_test | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/mdigger/jwt" | ||
) | ||
|
||
func Example() { | ||
// создаем шаблон и описываем в нем те вещи, которые | ||
// мы хотели бы включать во все токены | ||
tmpl := &jwt.Template{ | ||
Issuer: "me.mdigger.test", | ||
Expire: time.Hour, | ||
Created: true, | ||
Signer: jwt.NewSignerHS256([]byte(`top secret`)), | ||
} | ||
// описываем дополнительные поля токена (можно структурой) | ||
data := map[string]interface{}{ | ||
"user-id": "34529345", | ||
} | ||
// создаем и подписываем токен | ||
token, err := tmpl.Token(data) | ||
if err != nil { | ||
fmt.Println("Error creating:", err) | ||
return | ||
} | ||
// разбираем токен и получаем данные | ||
// если токен не валиден, то вернется ошибка | ||
if err := tmpl.Parse(token, &data); err != nil { | ||
fmt.Println("Error parsing:", err) | ||
return | ||
} | ||
} |
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,44 @@ | ||
package jwt | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"testing" | ||
) | ||
|
||
func TestSigner(t *testing.T) { | ||
signer := NewSignerHS256([]byte(`top secret`)) | ||
_, err := signer.Parse([]byte(`aaa.bbb`)) | ||
if err.Error() != "bad token parts" { | ||
t.Error("bad token parts") | ||
} | ||
_, err = signer.Parse([]byte(`aaa.bbb.ccc`)) | ||
if err == nil { | ||
t.Error("bad token encoder") | ||
} | ||
h, _ := json.Marshal(header{ | ||
Alg: "HS256", | ||
Typ: "JJJ", | ||
}) | ||
hh := base64.RawURLEncoding.EncodeToString(h) | ||
_, err = signer.Parse([]byte(hh + `1.bbb.ccc`)) | ||
if err == nil { | ||
t.Error("bad token header") | ||
} | ||
_, err = signer.Parse([]byte(hh + `.bbb.ccc`)) | ||
if err.Error() != "bad token type" { | ||
t.Error("bad token type") | ||
} | ||
_, err = signer.Parse([]byte(getHeader("none") + `.bbb.ccc`)) | ||
if err.Error() != "bad token sign algorithm" { | ||
t.Error("bad token sign algorithm") | ||
} | ||
_, err = signer.Parse([]byte(signer.header + `.bbb.ccc`)) | ||
if err.Error() != "bad token sign" { | ||
t.Error("bad token sign") | ||
} | ||
_, err = signer.Parse([]byte(signer.header + `.bbb.+++`)) | ||
if err == nil { | ||
t.Error("bad token sign data") | ||
} | ||
} |
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