From 7544643cfbf589a1d20f8955dbd3b1a72e268cea Mon Sep 17 00:00:00 2001 From: DROGOU Laurent Date: Sun, 14 Feb 2021 11:10:58 +0100 Subject: [PATCH] feat: ajout du refresh token --- model/oauth.go | 1 + routes.auth.go | 2 +- routes.go | 4 ++-- routes.jwt.go | 15 +++++++++------ routes.refresh.go | 26 ++++++++++++++++++++++++++ server.go | 1 + store/store.go | 4 +++- templateOAuth/resultat.go | 6 +++--- 8 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 routes.refresh.go diff --git a/model/oauth.go b/model/oauth.go index 2ca25d1..3e909ac 100644 --- a/model/oauth.go +++ b/model/oauth.go @@ -8,6 +8,7 @@ type Oauth struct { TokenType string `db:"token_type"` ExpiresIN float64 `db:"expires_in"` RefreshToken string `db:"refresh_token"` + Param Param `db:"param_id"` } func (o Oauth) String() string { diff --git a/routes.auth.go b/routes.auth.go index 19d33e3..a82036c 100644 --- a/routes.auth.go +++ b/routes.auth.go @@ -102,7 +102,7 @@ func (s *server) handleLocal() http.HandlerFunc { monID := strconv.Itoa(int(o.ID)) // Puis redisrect vers page resultat - rj := "http://localhost:8090/jwt?model=" + monID + rj := "http://localhost:8090/jwt/" + monID http.Redirect(rw, r, rj, http.StatusMovedPermanently) } diff --git a/routes.go b/routes.go index fbf41ab..4da0d54 100644 --- a/routes.go +++ b/routes.go @@ -5,7 +5,7 @@ func (s *server) routes() { s.router.HandleFunc("/oauth/redirect", s.handleRedirect()).Methods("GET") s.router.HandleFunc("/local", s.handleLocal()).Methods("POST") s.router.HandleFunc("/oauth20", s.handleOAuth20()).Methods("POST") - s.router.HandleFunc("/jwt", s.handleJSONWebToken()).Methods("GET") - s.router.HandleFunc("/oauth/refresh/{id}", s.handleRefreshToken()).Methods("POST") + s.router.HandleFunc("/jwt/{id}", s.handleJSONWebToken()).Methods("GET") + s.router.HandleFunc("/jwt/refresh/{id}", s.handleRefreshToken()).Methods("POST") } diff --git a/routes.jwt.go b/routes.jwt.go index 76fa94f..9e0bc37 100644 --- a/routes.jwt.go +++ b/routes.jwt.go @@ -12,6 +12,7 @@ import ( "strings" "github.com/dgrijalva/jwt-go" + "github.com/gorilla/mux" "github.com/ldrogou/goauth20/model" templateoauth "github.com/ldrogou/goauth20/templateOAuth" ) @@ -25,7 +26,6 @@ type JSONToken struct { Code string `json:"code"` } - func (s *server) handleRedirect() http.HandlerFunc { return func(rw http.ResponseWriter, r *http.Request) { @@ -94,7 +94,7 @@ func (s *server) handleRedirect() http.HandlerFunc { monID := strconv.Itoa(int(o.ID)) // Puis redisrect vers page resultat - rj := "http://localhost:8090/jwt?model=" + monID + rj := "http://localhost:8090/jwt/" + monID http.Redirect(rw, r, rj, http.StatusMovedPermanently) } } @@ -102,7 +102,11 @@ func (s *server) handleRedirect() http.HandlerFunc { func (s *server) handleJSONWebToken() http.HandlerFunc { return func(rw http.ResponseWriter, r *http.Request) { - c := r.URL.Query().Get("model") + vars, _ := mux.Vars(r)["id"] + jwtID, err := strconv.ParseInt(vars, 10, 64) + if err != nil { + log.Printf("erreur a la récupération id jwt (err=%v)", err) + } rw.Header().Set("Content-Type", "text/html") rw.WriteHeader(http.StatusOK) @@ -112,9 +116,7 @@ func (s *server) handleJSONWebToken() http.HandlerFunc { fmt.Printf("erreur suivante %v", err) } - oauthID, err := strconv.ParseInt(c, 10, 64) - - oauth, err := s.store.GetOauth(oauthID) + oauth, err := s.store.GetOauth(jwtID) if err != nil { log.Printf("erreur a la récupération oauth (err=%v)", err) } @@ -137,6 +139,7 @@ func (s *server) handleJSONWebToken() http.HandlerFunc { } f := File{ + JwtID: jwtID, JwtProduce: tokenVal, Header: string(header), Payload: string(payload), diff --git a/routes.refresh.go b/routes.refresh.go new file mode 100644 index 0000000..f5a13f1 --- /dev/null +++ b/routes.refresh.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "strconv" + + "github.com/gorilla/mux" +) + +func (s *server) handleRefreshToken() http.HandlerFunc { + return func(rw http.ResponseWriter, r *http.Request) { + + vars, _ := mux.Vars(r)["id"] + jwtID, err := strconv.ParseInt(vars, 10, 64) + if err != nil { + log.Printf("erreur a la récupération id jwt (err=%v)", err) + } + + fmt.Printf("le jwtID : %v", jwtID) + + // Puis redisrect vers page resultat + //s.response(rw, r, resp, http.StatusOK) + } +} diff --git a/server.go b/server.go index b1fa64f..b28963c 100644 --- a/server.go +++ b/server.go @@ -16,6 +16,7 @@ type server struct { //File structure du fichier type File struct { + JwtID int64 JwtProduce string Header string Payload string diff --git a/store/store.go b/store/store.go index d74d401..206f7c6 100644 --- a/store/store.go +++ b/store/store.go @@ -32,7 +32,9 @@ CREATE TABLE IF NOT EXISTS oauth access_token TEXT, token_type TEXT, expires_in INTEGER, - refresh_token TEXT + refresh_token TEXT, + param_id INTEGER, + FOREIGN KEY(param_id) REFERENCES param(id) ) ` diff --git a/templateOAuth/resultat.go b/templateOAuth/resultat.go index 81c0efe..3676668 100644 --- a/templateOAuth/resultat.go +++ b/templateOAuth/resultat.go @@ -20,9 +20,9 @@ var Resultat = ` navigator.clipboard.writeText(jwt) } - function test(){ + function test(jwtID){ var xhr = new XMLHttpRequest(); - xhr.open('GET', 'http://localhost:8080/oauth/refresh/1'); + xhr.open('GET', 'http://localhost:8080/jwt/refresh/' + jwtID); xhr.onreadystatechange = function() { if (xhr.readyState === 4) { alert(xhr.responseText); @@ -41,7 +41,7 @@ var Resultat = ` content_copy - + refreshRefresh Token