Skip to content

Commit 95f924a

Browse files
authored
Merge pull request #35 from grounded042/fix-old-package-ref
import form3tech-oss/jwt-go instead of dgrijalva/jwt-go to match the middleware
2 parents 21d9d59 + f9e5022 commit 95f924a

2 files changed

Lines changed: 16 additions & 19 deletions

File tree

01-Authorization-RS256/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Golang Authorization for RS256-Signed Tokens
22

3-
> :warning: **Important security note:** This solution uses a 3rd party library with an unresolved [security issue](https://cve.mitre.org/cgi-bin/cvename.cgi?name=2020-26160). Please review the details of the vulnerability, including any of the documented mitigations, before implementing the solution.
4-
53
This sample demonstrates how to protect endpoints in a Go API by verifying an incoming JWT access token signed by Auth0. The token must be signed with the RS256 algorithm and must be verified against your Auth0 JSON Web Key Set.
64

75
## Getting Started

01-Authorization-RS256/main.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ package main
22

33
import (
44
"encoding/json"
5-
"fmt"
6-
"net/http"
7-
"strings"
85
"errors"
6+
"fmt"
97
"log"
8+
"net/http"
109
"os"
10+
"strings"
1111

12+
jwtmiddleware "github.com/auth0/go-jwt-middleware"
1213
"github.com/codegangsta/negroni"
13-
"github.com/auth0/go-jwt-middleware"
14-
"github.com/dgrijalva/jwt-go"
14+
"github.com/form3tech-oss/jwt-go"
1515
"github.com/gorilla/mux"
1616
"github.com/joho/godotenv"
1717
"github.com/rs/cors"
@@ -26,11 +26,11 @@ type Jwks struct {
2626
}
2727

2828
type JSONWebKeys struct {
29-
Kty string `json:"kty"`
30-
Kid string `json:"kid"`
31-
Use string `json:"use"`
32-
N string `json:"n"`
33-
E string `json:"e"`
29+
Kty string `json:"kty"`
30+
Kid string `json:"kid"`
31+
Use string `json:"use"`
32+
N string `json:"n"`
33+
E string `json:"e"`
3434
X5c []string `json:"x5c"`
3535
}
3636

@@ -41,7 +41,7 @@ func main() {
4141
log.Print("Error loading .env file")
4242
}
4343

44-
jwtMiddleware := jwtmiddleware.New(jwtmiddleware.Options {
44+
jwtMiddleware := jwtmiddleware.New(jwtmiddleware.Options{
4545
ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
4646
// Verify 'aud' claim
4747
aud := os.Getenv("AUTH0_AUDIENCE")
@@ -68,9 +68,9 @@ func main() {
6868
})
6969

7070
c := cors.New(cors.Options{
71-
AllowedOrigins: []string{"http://localhost:3000"},
71+
AllowedOrigins: []string{"http://localhost:3000"},
7272
AllowCredentials: true,
73-
AllowedHeaders: []string{"Authorization"},
73+
AllowedHeaders: []string{"Authorization"},
7474
})
7575

7676
r := mux.NewRouter()
@@ -89,7 +89,7 @@ func main() {
8989
negroni.Wrap(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
9090
message := "Hello from a private endpoint! You need to be authenticated to see this."
9191
responseJSON(message, w, http.StatusOK)
92-
}))))
92+
}))))
9393

9494
// This route is only accessible if the user has a valid access_token with the read:messages scope
9595
// We are chaining the jwtmiddleware middleware into the negroni handler function which will check
@@ -109,22 +109,21 @@ func main() {
109109
}
110110
message := "Hello from a private endpoint! You need to be authenticated to see this."
111111
responseJSON(message, w, http.StatusOK)
112-
}))))
112+
}))))
113113

114114
handler := c.Handler(r)
115115
http.Handle("/", r)
116116
fmt.Println("Listening on http://localhost:3010")
117117
http.ListenAndServe("0.0.0.0:3010", handler)
118118
}
119119

120-
121120
type CustomClaims struct {
122121
Scope string `json:"scope"`
123122
jwt.StandardClaims
124123
}
125124

126125
func checkScope(scope string, tokenString string) bool {
127-
token, _ := jwt.ParseWithClaims(tokenString, &CustomClaims{}, func (token *jwt.Token) (interface{}, error) {
126+
token, _ := jwt.ParseWithClaims(tokenString, &CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
128127
cert, err := getPemCert(token)
129128
if err != nil {
130129
return nil, err

0 commit comments

Comments
 (0)