feat: ajout du refresh token

This commit is contained in:
2021-02-14 11:10:58 +01:00
parent 089b5985f8
commit 7544643cfb
8 changed files with 46 additions and 13 deletions

View File

@@ -8,6 +8,7 @@ type Oauth struct {
TokenType string `db:"token_type"` TokenType string `db:"token_type"`
ExpiresIN float64 `db:"expires_in"` ExpiresIN float64 `db:"expires_in"`
RefreshToken string `db:"refresh_token"` RefreshToken string `db:"refresh_token"`
Param Param `db:"param_id"`
} }
func (o Oauth) String() string { func (o Oauth) String() string {

View File

@@ -102,7 +102,7 @@ func (s *server) handleLocal() http.HandlerFunc {
monID := strconv.Itoa(int(o.ID)) monID := strconv.Itoa(int(o.ID))
// Puis redisrect vers page resultat // 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) http.Redirect(rw, r, rj, http.StatusMovedPermanently)
} }

View File

@@ -5,7 +5,7 @@ func (s *server) routes() {
s.router.HandleFunc("/oauth/redirect", s.handleRedirect()).Methods("GET") s.router.HandleFunc("/oauth/redirect", s.handleRedirect()).Methods("GET")
s.router.HandleFunc("/local", s.handleLocal()).Methods("POST") s.router.HandleFunc("/local", s.handleLocal()).Methods("POST")
s.router.HandleFunc("/oauth20", s.handleOAuth20()).Methods("POST") s.router.HandleFunc("/oauth20", s.handleOAuth20()).Methods("POST")
s.router.HandleFunc("/jwt", s.handleJSONWebToken()).Methods("GET") s.router.HandleFunc("/jwt/{id}", s.handleJSONWebToken()).Methods("GET")
s.router.HandleFunc("/oauth/refresh/{id}", s.handleRefreshToken()).Methods("POST") s.router.HandleFunc("/jwt/refresh/{id}", s.handleRefreshToken()).Methods("POST")
} }

View File

@@ -12,6 +12,7 @@ import (
"strings" "strings"
"github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go"
"github.com/gorilla/mux"
"github.com/ldrogou/goauth20/model" "github.com/ldrogou/goauth20/model"
templateoauth "github.com/ldrogou/goauth20/templateOAuth" templateoauth "github.com/ldrogou/goauth20/templateOAuth"
) )
@@ -25,7 +26,6 @@ type JSONToken struct {
Code string `json:"code"` Code string `json:"code"`
} }
func (s *server) handleRedirect() http.HandlerFunc { func (s *server) handleRedirect() http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) { return func(rw http.ResponseWriter, r *http.Request) {
@@ -94,7 +94,7 @@ func (s *server) handleRedirect() http.HandlerFunc {
monID := strconv.Itoa(int(o.ID)) monID := strconv.Itoa(int(o.ID))
// Puis redisrect vers page resultat // 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) http.Redirect(rw, r, rj, http.StatusMovedPermanently)
} }
} }
@@ -102,7 +102,11 @@ func (s *server) handleRedirect() http.HandlerFunc {
func (s *server) handleJSONWebToken() http.HandlerFunc { func (s *server) handleJSONWebToken() http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) { 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.Header().Set("Content-Type", "text/html")
rw.WriteHeader(http.StatusOK) rw.WriteHeader(http.StatusOK)
@@ -112,9 +116,7 @@ func (s *server) handleJSONWebToken() http.HandlerFunc {
fmt.Printf("erreur suivante %v", err) fmt.Printf("erreur suivante %v", err)
} }
oauthID, err := strconv.ParseInt(c, 10, 64) oauth, err := s.store.GetOauth(jwtID)
oauth, err := s.store.GetOauth(oauthID)
if err != nil { if err != nil {
log.Printf("erreur a la récupération oauth (err=%v)", err) log.Printf("erreur a la récupération oauth (err=%v)", err)
} }
@@ -137,6 +139,7 @@ func (s *server) handleJSONWebToken() http.HandlerFunc {
} }
f := File{ f := File{
JwtID: jwtID,
JwtProduce: tokenVal, JwtProduce: tokenVal,
Header: string(header), Header: string(header),
Payload: string(payload), Payload: string(payload),

26
routes.refresh.go Normal file
View 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)
}
}

View File

@@ -16,6 +16,7 @@ type server struct {
//File structure du fichier //File structure du fichier
type File struct { type File struct {
JwtID int64
JwtProduce string JwtProduce string
Header string Header string
Payload string Payload string

View File

@@ -32,7 +32,9 @@ CREATE TABLE IF NOT EXISTS oauth
access_token TEXT, access_token TEXT,
token_type TEXT, token_type TEXT,
expires_in INTEGER, expires_in INTEGER,
refresh_token TEXT refresh_token TEXT,
param_id INTEGER,
FOREIGN KEY(param_id) REFERENCES param(id)
) )
` `

View File

@@ -20,9 +20,9 @@ var Resultat = `<!DOCTYPE html>
navigator.clipboard.writeText(jwt) navigator.clipboard.writeText(jwt)
} }
function test(){ function test(jwtID){
var xhr = new XMLHttpRequest(); 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() { xhr.onreadystatechange = function() {
if (xhr.readyState === 4) { if (xhr.readyState === 4) {
alert(xhr.responseText); 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 }}');" > <a class="waves-effect waves-light btn tooltipped" data-tooltip="Copy" onclick="copy('{{.JwtProduce }}');" >
<i class="material-icons center">content_copy</i> <i class="material-icons center">content_copy</i>
</a> </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 <i class="material-icons left bottom">refresh</i>Refresh Token
</a> </a>
<div class="row"> <div class="row">