Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
G
golang-github-golang-jwt-jwt-v5
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Debian Go Packaging Team
packages
golang-github-golang-jwt-jwt-v5
Commits
c776b832
Unverified
Commit
c776b832
authored
1 year ago
by
Craig Pastro
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Add error handling to examples (#312)
parent
908d3567
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
cmd/jwt/main.go
+5
-10
5 additions, 10 deletions
cmd/jwt/main.go
example_test.go
+18
-14
18 additions, 14 deletions
example_test.go
hmac_example_test.go
+5
-1
5 additions, 1 deletion
hmac_example_test.go
http_example_test.go
+1
-3
1 addition, 3 deletions
http_example_test.go
with
29 additions
and
28 deletions
cmd/jwt/main.go
+
5
−
10
View file @
c776b832
...
@@ -150,20 +150,15 @@ func verifyToken() error {
...
@@ -150,20 +150,15 @@ func verifyToken() error {
}
}
})
})
// Print some debug data
if
*
flagDebug
&&
token
!=
nil
{
fmt
.
Fprintf
(
os
.
Stderr
,
"Header:
\n
%v
\n
"
,
token
.
Header
)
fmt
.
Fprintf
(
os
.
Stderr
,
"Claims:
\n
%v
\n
"
,
token
.
Claims
)
}
// Print an error if we can't parse for some reason
// Print an error if we can't parse for some reason
if
err
!=
nil
{
if
err
!=
nil
{
return
fmt
.
Errorf
(
"couldn't parse token: %w"
,
err
)
return
fmt
.
Errorf
(
"couldn't parse token: %w"
,
err
)
}
}
// Is token invalid?
// Print some debug data
if
!
token
.
Valid
{
if
*
flagDebug
{
return
fmt
.
Errorf
(
"token is invalid"
)
fmt
.
Fprintf
(
os
.
Stderr
,
"Header:
\n
%v
\n
"
,
token
.
Header
)
fmt
.
Fprintf
(
os
.
Stderr
,
"Claims:
\n
%v
\n
"
,
token
.
Claims
)
}
}
// Print the token details
// Print the token details
...
@@ -279,7 +274,7 @@ func showToken() error {
...
@@ -279,7 +274,7 @@ func showToken() error {
}
}
token
,
err
:=
jwt
.
Parse
(
string
(
tokData
),
nil
)
token
,
err
:=
jwt
.
Parse
(
string
(
tokData
),
nil
)
if
token
=
=
nil
{
if
err
!
=
nil
{
return
fmt
.
Errorf
(
"malformed token: %w"
,
err
)
return
fmt
.
Errorf
(
"malformed token: %w"
,
err
)
}
}
...
...
This diff is collapsed.
Click to expand it.
example_test.go
+
18
−
14
View file @
c776b832
...
@@ -3,6 +3,7 @@ package jwt_test
...
@@ -3,6 +3,7 @@ package jwt_test
import
(
import
(
"errors"
"errors"
"fmt"
"fmt"
"log"
"time"
"time"
"github.com/golang-jwt/jwt/v5"
"github.com/golang-jwt/jwt/v5"
...
@@ -24,7 +25,7 @@ func ExampleNewWithClaims_registeredClaims() {
...
@@ -24,7 +25,7 @@ func ExampleNewWithClaims_registeredClaims() {
token
:=
jwt
.
NewWithClaims
(
jwt
.
SigningMethodHS256
,
claims
)
token
:=
jwt
.
NewWithClaims
(
jwt
.
SigningMethodHS256
,
claims
)
ss
,
err
:=
token
.
SignedString
(
mySigningKey
)
ss
,
err
:=
token
.
SignedString
(
mySigningKey
)
fmt
.
Print
f
(
"%v %v"
,
ss
,
err
)
fmt
.
Print
ln
(
ss
,
err
)
// Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0ZXN0IiwiZXhwIjoxNTE2MjM5MDIyfQ.0XN_1Tpp9FszFOonIBpwha0c_SfnNI22DhTnjMshPg8 <nil>
// Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0ZXN0IiwiZXhwIjoxNTE2MjM5MDIyfQ.0XN_1Tpp9FszFOonIBpwha0c_SfnNI22DhTnjMshPg8 <nil>
}
}
...
@@ -67,7 +68,7 @@ func ExampleNewWithClaims_customClaimsType() {
...
@@ -67,7 +68,7 @@ func ExampleNewWithClaims_customClaimsType() {
token
:=
jwt
.
NewWithClaims
(
jwt
.
SigningMethodHS256
,
claims
)
token
:=
jwt
.
NewWithClaims
(
jwt
.
SigningMethodHS256
,
claims
)
ss
,
err
:=
token
.
SignedString
(
mySigningKey
)
ss
,
err
:=
token
.
SignedString
(
mySigningKey
)
fmt
.
Print
f
(
"%v %v"
,
ss
,
err
)
fmt
.
Print
ln
(
ss
,
err
)
// Output: foo: bar
// Output: foo: bar
// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJpc3MiOiJ0ZXN0IiwiZXhwIjoxNTE2MjM5MDIyfQ.xVuY2FZ_MRXMIEgVQ7J-TFtaucVFRXUzHm9LmV41goM <nil>
// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJpc3MiOiJ0ZXN0IiwiZXhwIjoxNTE2MjM5MDIyfQ.xVuY2FZ_MRXMIEgVQ7J-TFtaucVFRXUzHm9LmV41goM <nil>
...
@@ -86,11 +87,12 @@ func ExampleParseWithClaims_customClaimsType() {
...
@@ -86,11 +87,12 @@ func ExampleParseWithClaims_customClaimsType() {
token
,
err
:=
jwt
.
ParseWithClaims
(
tokenString
,
&
MyCustomClaims
{},
func
(
token
*
jwt
.
Token
)
(
interface
{},
error
)
{
token
,
err
:=
jwt
.
ParseWithClaims
(
tokenString
,
&
MyCustomClaims
{},
func
(
token
*
jwt
.
Token
)
(
interface
{},
error
)
{
return
[]
byte
(
"AllYourBase"
),
nil
return
[]
byte
(
"AllYourBase"
),
nil
})
})
if
err
!=
nil
{
if
claims
,
ok
:=
token
.
Claims
.
(
*
MyCustomClaims
);
ok
&&
token
.
Valid
{
log
.
Fatal
(
err
)
fmt
.
Printf
(
"%v %v"
,
claims
.
Foo
,
claims
.
RegisteredClaims
.
Issuer
)
}
else
if
claims
,
ok
:=
token
.
Claims
.
(
*
MyCustomClaims
);
ok
{
fmt
.
Println
(
claims
.
Foo
,
claims
.
RegisteredClaims
.
Issuer
)
}
else
{
}
else
{
fmt
.
Println
(
err
)
log
.
Fatal
(
"unknown claims type, cannot proceed"
)
}
}
// Output: bar test
// Output: bar test
...
@@ -109,11 +111,12 @@ func ExampleParseWithClaims_validationOptions() {
...
@@ -109,11 +111,12 @@ func ExampleParseWithClaims_validationOptions() {
token
,
err
:=
jwt
.
ParseWithClaims
(
tokenString
,
&
MyCustomClaims
{},
func
(
token
*
jwt
.
Token
)
(
interface
{},
error
)
{
token
,
err
:=
jwt
.
ParseWithClaims
(
tokenString
,
&
MyCustomClaims
{},
func
(
token
*
jwt
.
Token
)
(
interface
{},
error
)
{
return
[]
byte
(
"AllYourBase"
),
nil
return
[]
byte
(
"AllYourBase"
),
nil
},
jwt
.
WithLeeway
(
5
*
time
.
Second
))
},
jwt
.
WithLeeway
(
5
*
time
.
Second
))
if
err
!=
nil
{
if
claims
,
ok
:=
token
.
Claims
.
(
*
MyCustomClaims
);
ok
&&
token
.
Valid
{
log
.
Fatal
(
err
)
fmt
.
Printf
(
"%v %v"
,
claims
.
Foo
,
claims
.
RegisteredClaims
.
Issuer
)
}
else
if
claims
,
ok
:=
token
.
Claims
.
(
*
MyCustomClaims
);
ok
{
fmt
.
Println
(
claims
.
Foo
,
claims
.
RegisteredClaims
.
Issuer
)
}
else
{
}
else
{
fmt
.
Println
(
err
)
log
.
Fatal
(
"unknown claims type, cannot proceed"
)
}
}
// Output: bar test
// Output: bar test
...
@@ -147,11 +150,12 @@ func ExampleParseWithClaims_customValidation() {
...
@@ -147,11 +150,12 @@ func ExampleParseWithClaims_customValidation() {
token
,
err
:=
jwt
.
ParseWithClaims
(
tokenString
,
&
MyCustomClaims
{},
func
(
token
*
jwt
.
Token
)
(
interface
{},
error
)
{
token
,
err
:=
jwt
.
ParseWithClaims
(
tokenString
,
&
MyCustomClaims
{},
func
(
token
*
jwt
.
Token
)
(
interface
{},
error
)
{
return
[]
byte
(
"AllYourBase"
),
nil
return
[]
byte
(
"AllYourBase"
),
nil
},
jwt
.
WithLeeway
(
5
*
time
.
Second
))
},
jwt
.
WithLeeway
(
5
*
time
.
Second
))
if
err
!=
nil
{
if
claims
,
ok
:=
token
.
Claims
.
(
*
MyCustomClaims
);
ok
&&
token
.
Valid
{
log
.
Fatal
(
err
)
fmt
.
Printf
(
"%v %v"
,
claims
.
Foo
,
claims
.
RegisteredClaims
.
Issuer
)
}
else
if
claims
,
ok
:=
token
.
Claims
.
(
*
MyCustomClaims
);
ok
{
fmt
.
Println
(
claims
.
Foo
,
claims
.
RegisteredClaims
.
Issuer
)
}
else
{
}
else
{
fmt
.
Println
(
err
)
log
.
Fatal
(
"unknown claims type, cannot proceed"
)
}
}
// Output: bar test
// Output: bar test
...
...
This diff is collapsed.
Click to expand it.
hmac_example_test.go
+
5
−
1
View file @
c776b832
...
@@ -2,6 +2,7 @@ package jwt_test
...
@@ -2,6 +2,7 @@ package jwt_test
import
(
import
(
"fmt"
"fmt"
"log"
"os"
"os"
"time"
"time"
...
@@ -56,8 +57,11 @@ func ExampleParse_hmac() {
...
@@ -56,8 +57,11 @@ func ExampleParse_hmac() {
// hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key")
// hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key")
return
hmacSampleSecret
,
nil
return
hmacSampleSecret
,
nil
})
})
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
if
claims
,
ok
:=
token
.
Claims
.
(
jwt
.
MapClaims
);
ok
&&
token
.
Valid
{
if
claims
,
ok
:=
token
.
Claims
.
(
jwt
.
MapClaims
);
ok
{
fmt
.
Println
(
claims
[
"foo"
],
claims
[
"nbf"
])
fmt
.
Println
(
claims
[
"foo"
],
claims
[
"nbf"
])
}
else
{
}
else
{
fmt
.
Println
(
err
)
fmt
.
Println
(
err
)
...
...
This diff is collapsed.
Click to expand it.
http_example_test.go
+
1
−
3
View file @
c776b832
...
@@ -83,9 +83,7 @@ func Example_getTokenViaHTTP() {
...
@@ -83,9 +83,7 @@ func Example_getTokenViaHTTP() {
"user"
:
{
"test"
},
"user"
:
{
"test"
},
"pass"
:
{
"known"
},
"pass"
:
{
"known"
},
})
})
if
err
!=
nil
{
fatal
(
err
)
fatal
(
err
)
}
if
res
.
StatusCode
!=
200
{
if
res
.
StatusCode
!=
200
{
fmt
.
Println
(
"Unexpected status code"
,
res
.
StatusCode
)
fmt
.
Println
(
"Unexpected status code"
,
res
.
StatusCode
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment