ajout template resultat
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
@@ -9,6 +10,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/dgrijalva/jwt-go"
|
"github.com/dgrijalva/jwt-go"
|
||||||
@@ -16,8 +18,7 @@ import (
|
|||||||
|
|
||||||
//File structure du fichier
|
//File structure du fichier
|
||||||
type File struct {
|
type File struct {
|
||||||
Name string
|
jwtProduce string
|
||||||
Other string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type JsonToken struct {
|
type JsonToken struct {
|
||||||
@@ -55,28 +56,20 @@ func (s *server) handleIndex() http.HandlerFunc {
|
|||||||
fmt.Errorf("erreur suivante %v", err)
|
fmt.Errorf("erreur suivante %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
f := File{Name: "Drogou", Other: "Dans le fichier"}
|
err = t.Execute(rw, nil)
|
||||||
|
|
||||||
err = t.Execute(rw, f)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Errorf("erreur suivante %v", err)
|
fmt.Errorf("erreur suivante %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
func (s *server) handleTest() http.HandlerFunc {
|
func (s *server) handleLocal() http.HandlerFunc {
|
||||||
return func(rw http.ResponseWriter, r *http.Request) {
|
return func(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
fmt.Println("sub")
|
|
||||||
|
|
||||||
sub := r.FormValue("sub")
|
sub := r.FormValue("sub")
|
||||||
fmt.Printf("sub %v", sub)
|
|
||||||
idEntreprise := r.FormValue("id_entreprise")
|
idEntreprise := r.FormValue("id_entreprise")
|
||||||
fmt.Printf("idEntreprise %v", idEntreprise)
|
|
||||||
rcaPartnerID := r.FormValue("rcaPartnerId")
|
rcaPartnerID := r.FormValue("rcaPartnerId")
|
||||||
fmt.Printf("rcaPartnerID %v", rcaPartnerID)
|
|
||||||
var jwtKey = []byte(r.FormValue("secret"))
|
var jwtKey = []byte(r.FormValue("secret"))
|
||||||
fmt.Printf("secret %v", jwtKey)
|
|
||||||
|
|
||||||
// Declare the expiration time of the token
|
// Declare the expiration time of the token
|
||||||
// here, we have kept it as 5 minutes
|
// here, we have kept it as 5 minutes
|
||||||
@@ -96,26 +89,60 @@ func (s *server) handleTest() http.HandlerFunc {
|
|||||||
ExpiresAt: expirationTime.Unix(),
|
ExpiresAt: expirationTime.Unix(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
fmt.Printf("claims %v", claims)
|
|
||||||
|
|
||||||
// Declare the token with the algorithm used for signing, and the claims
|
// Declare the token with the algorithm used for signing, and the claims
|
||||||
tokenstr := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
tokenstr := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||||
fmt.Printf("token %v", tokenstr)
|
zer, _ := json.Marshal(tokenstr.Claims)
|
||||||
|
fmt.Printf("zer %v", string(zer))
|
||||||
|
|
||||||
// Create the JWT string
|
// Create the JWT string
|
||||||
tokenString, err := tokenstr.SignedString(jwtKey)
|
tokenString, err := tokenstr.SignedString(jwtKey)
|
||||||
fmt.Printf("tokenString %v", tokenString)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("erreur %v", err)
|
log.Printf("erreur %v", err)
|
||||||
// If there is an error in creating the JWT return an internal server error
|
// If there is an error in creating the JWT return an internal server error
|
||||||
rw.WriteHeader(http.StatusInternalServerError)
|
rw.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
log.Printf("le token %v \n", tokenString)
|
||||||
|
tableau := strings.Split(tokenString, ".")
|
||||||
|
log.Println(tableau[0])
|
||||||
|
headerrr, _ := base64.URLEncoding.DecodeString(tableau[0])
|
||||||
|
log.Println(string(string(headerrr)))
|
||||||
|
|
||||||
s.response(rw, r, tokenString, http.StatusOK)
|
log.Println(tableau[1])
|
||||||
|
claimssss, _ := base64.URLEncoding.DecodeString(tableau[1])
|
||||||
|
log.Println(string(string(claimssss)))
|
||||||
|
|
||||||
|
log.Println(tableau[2])
|
||||||
|
test, _ := base64.URLEncoding.DecodeString(tableau[2])
|
||||||
|
log.Println(string(string(test)))
|
||||||
|
|
||||||
|
s.response(rw, r, string(zer), http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *server) handleOAuth20() http.HandlerFunc {
|
||||||
|
return func(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
domain := r.FormValue("domain")
|
||||||
|
clientID := r.FormValue("clientId")
|
||||||
|
scopes := r.FormValue("scopes")
|
||||||
|
currentCompany := r.FormValue("currentCompany")
|
||||||
|
if len(currentCompany) == 0 {
|
||||||
|
currentCompany = "false"
|
||||||
|
} else {
|
||||||
|
currentCompany = "true"
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println(currentCompany)
|
||||||
|
redirecthttp := "https://" + domain + "/entreprise-partenaire/authorize?client_id=" + clientID + "&scope=" + scopes + "¤t_company=" + currentCompany + "&redirect_uri=http://localhost:8080/oauth/redirect"
|
||||||
|
http.Redirect(rw, r, redirecthttp, http.StatusMovedPermanently)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
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) {
|
||||||
|
|
||||||
@@ -154,15 +181,13 @@ func (s *server) handleRedirect() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
tokenVal := t.(interface{}).(map[string]interface{})
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Cannot parse token body err=%v", err)
|
log.Printf("Cannot parse token body err=%v", err)
|
||||||
s.response(rw, r, nil, http.StatusBadGateway)
|
s.response(rw, r, nil, http.StatusBadGateway)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
s.response(rw, r, tokenVal["access_token"], http.StatusOK)
|
s.responseFile(rw, r, t, http.StatusOK)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package main
|
|||||||
func (s *server) routes() {
|
func (s *server) routes() {
|
||||||
s.router.HandleFunc("/index", s.handleIndex()).Methods("GET")
|
s.router.HandleFunc("/index", s.handleIndex()).Methods("GET")
|
||||||
s.router.HandleFunc("/oauth/redirect", s.handleRedirect()).Methods("GET")
|
s.router.HandleFunc("/oauth/redirect", s.handleRedirect()).Methods("GET")
|
||||||
s.router.HandleFunc("/test", s.handleTest()).Methods("POST")
|
s.router.HandleFunc("/local", s.handleLocal()).Methods("POST")
|
||||||
|
s.router.HandleFunc("/oauth20", s.handleOAuth20()).Methods("POST")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
27
server.go
27
server.go
@@ -2,6 +2,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -40,6 +42,31 @@ func (s *server) response(rw http.ResponseWriter, _ *http.Request, data interfac
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *server) responseFile(rw http.ResponseWriter, _ *http.Request, data interface{}, status int) {
|
||||||
|
rw.Header().Set("Content-Type", "text/html")
|
||||||
|
rw.WriteHeader(status)
|
||||||
|
|
||||||
|
tokenVal := data.(interface{}).(map[string]interface{})
|
||||||
|
|
||||||
|
//t := template.New("mon template")
|
||||||
|
tem, err := template.ParseFiles("template/resultat.html")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Errorf("erreur suivante %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
sssss := tokenVal["access_token"].(string)
|
||||||
|
//sssss := "erer"
|
||||||
|
log.Println(sssss)
|
||||||
|
|
||||||
|
f := File{jwtProduce: sssss}
|
||||||
|
|
||||||
|
err = tem.Execute(rw, f)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Errorf("erreur suivante %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (s *server) decode(rw http.ResponseWriter, r *http.Request, v interface{}) error {
|
func (s *server) decode(rw http.ResponseWriter, r *http.Request, v interface{}) error {
|
||||||
return json.NewDecoder(r.Body).Decode(v)
|
return json.NewDecoder(r.Body).Decode(v)
|
||||||
|
|
||||||
|
|||||||
@@ -28,8 +28,8 @@
|
|||||||
return encodedSource;
|
return encodedSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateToken() {
|
function generateToken(form) {
|
||||||
var form = document.getElementById("monform");
|
var form = document.getElementById(form);
|
||||||
|
|
||||||
form.submit();
|
form.submit();
|
||||||
|
|
||||||
@@ -44,12 +44,12 @@
|
|||||||
var data = {
|
var data = {
|
||||||
"sub": document.getElementById('sub').value,
|
"sub": document.getElementById('sub').value,
|
||||||
"exp": Math.floor(Date.now() / 1000) + 6 * 30 * 24 * 3600,
|
"exp": Math.floor(Date.now() / 1000) + 6 * 30 * 24 * 3600,
|
||||||
"roles": [
|
"roles": [
|
||||||
"RCA_CLOUD_EXPERT_COMPTABLE",
|
"RCA_CLOUD_EXPERT_COMPTABLE",
|
||||||
"E_COLLECTE_BO_CREA",
|
"E_COLLECTE_BO_CREA",
|
||||||
"E_CREATION_CREA",
|
"E_CREATION_CREA",
|
||||||
"E_QUESTIONNAIRE_CREA"
|
"E_QUESTIONNAIRE_CREA"
|
||||||
],
|
],
|
||||||
"id_entreprise": document.getElementById('id_entreprise').value,
|
"id_entreprise": document.getElementById('id_entreprise').value,
|
||||||
"rcaPartnerId": document.getElementById('rcaPartnerId').value
|
"rcaPartnerId": document.getElementById('rcaPartnerId').value
|
||||||
};
|
};
|
||||||
@@ -75,9 +75,12 @@
|
|||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
<div>
|
||||||
|
<h1 class="center-align">JWT</h1>
|
||||||
|
</div>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<form class="col s12" id="monform" method="post" action="/test">
|
<form class="col s6 light-blue lighten-5" id="formLocal" method="post" action="/local">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="input-field col s12">
|
<div class="input-field col s12">
|
||||||
<i class="material-icons prefix">account_circle</i>
|
<i class="material-icons prefix">account_circle</i>
|
||||||
@@ -107,21 +110,57 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<a class="waves-effect waves-light btn" onclick="generateToken();"><i class="material-icons left">cloud</i>Generer</a>
|
<a class="waves-effect waves-light btn" onclick="generateToken('formLocal');"><i
|
||||||
|
class="material-icons left">cloud</i>Local</a>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
|
||||||
<div class="row">
|
<form class="col s6 light-green lighten-5" id="formOAtuh20" method="post" action="/oauth20">
|
||||||
<form class="col s12">
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="input-field col s12">
|
<div class="input-field col s12">
|
||||||
<textarea id="jwt" class="materialize-textarea"></textarea>
|
<i class="material-icons prefix">account_balance</i>
|
||||||
<label for="textarea1">JWT</label>
|
<input type="text" id="domain" name="domain" value="captation.beta.rca.fr">
|
||||||
|
<label for="name">Domaine :</label>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="input-field col s12">
|
||||||
|
<i class="material-icons prefix">account_balance</i>
|
||||||
|
<input type="text" id="clientId" name="clientId" value="meg-test-interne">
|
||||||
|
<label for="name">Client Id :</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="input-field col s12">
|
||||||
|
<i class="material-icons prefix">account_balance</i>
|
||||||
|
<input type="text" id="clientSecret" name="clientSecret" value="xxxxxxxx">
|
||||||
|
<label for="name">Client Secret :</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="input-field col s12">
|
||||||
|
<i class="material-icons prefix">account_balance</i>
|
||||||
|
<input type="text" id="scopes" name="scopes" value="user">
|
||||||
|
<label for="name">Scopes</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="checkbox col s12">
|
||||||
|
<label>
|
||||||
|
<input type="checkbox" id="currentCompany" name="currentCompany" checked="checked" />
|
||||||
|
<span>Company courante</span>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<a class="waves-effect waves-light btn" onclick="generateToken('formOAtuh20');"><i
|
||||||
|
class="material-icons left">cloud</i>OAuth2.0</a>
|
||||||
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
33
template/resultat.html
Normal file
33
template/resultat.html
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<title>RCA JWT API</title>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
|
||||||
|
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
|
||||||
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
|
||||||
|
|
||||||
|
<script src="http://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/hmac-sha512.js"></script>
|
||||||
|
<script src="http://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/enc-base64-min.js"></script>
|
||||||
|
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div>
|
||||||
|
<h1 class="center-align">Composition</h1>
|
||||||
|
</div>
|
||||||
|
<div class="container">
|
||||||
|
|
||||||
|
{{.jwtProduce }}
|
||||||
|
|
||||||
|
<div class="row">
|
||||||
|
<div class="input-field col s12">
|
||||||
|
<i class="material-icons prefix">account_circle</i>
|
||||||
|
<label for="name">{{.jwtProduce }}</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user