feat: ajout du refresh token
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
|
||||
26
routes.refresh.go
Normal file
26
routes.refresh.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ type server struct {
|
||||
|
||||
//File structure du fichier
|
||||
type File struct {
|
||||
JwtID int64
|
||||
JwtProduce string
|
||||
Header string
|
||||
Payload string
|
||||
|
||||
@@ -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)
|
||||
)
|
||||
`
|
||||
|
||||
|
||||
@@ -20,9 +20,9 @@ var Resultat = `<!DOCTYPE html>
|
||||
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 = `<!DOCTYPE html>
|
||||
<a class="waves-effect waves-light btn tooltipped" data-tooltip="Copy" onclick="copy('{{.JwtProduce }}');" >
|
||||
<i class="material-icons center">content_copy</i>
|
||||
</a>
|
||||
<a class="waves-effect waves-light btn tooltipped" data-tooltip="Refresh" onclick="refresh('{{.JwtProduce }}');" >
|
||||
<a class="waves-effect waves-light btn tooltipped" data-tooltip="Refresh" onclick="refresh('{{.JwtID }}');" >
|
||||
<i class="material-icons left bottom">refresh</i>Refresh Token
|
||||
</a>
|
||||
<div class="row">
|
||||
|
||||
Reference in New Issue
Block a user