Skip to content

Commit

Permalink
crypto: add JWT Payload Decoding sample (vlang#23275)
Browse files Browse the repository at this point in the history
  • Loading branch information
KeitoTobi1 authored Dec 26, 2024
1 parent 7eec8b1 commit f8c9edd
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion vlib/crypto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ fn main() {
secret := 'your-256-bit-secret'
token := make_token(secret)
ok := auth_verify(secret, token)
pl := decode_payload(token) or { panic(err) }
dt := sw.elapsed().microseconds()
println('token: ${token}')
println('auth_verify(secret, token): ${ok}')
println('decode_payload(token): ${pl}')
println('Elapsed time: ${dt} uS')
}
Expand All @@ -93,4 +95,10 @@ fn auth_verify(secret string, token string) bool {
signature_from_token := base64.url_decode(token_split[2])
return hmac.equal(signature_from_token, signature_mirror)
}
```
fn decode_payload(token string) !JwtPayload {
token_split := token.split('.')
payload := json.decode(JwtPayload, base64.url_decode_str(token_split[1]))!
return payload
}
```

0 comments on commit f8c9edd

Please sign in to comment.