From 76d2a2f893389c20931480dfdeceaf9c48a7391b Mon Sep 17 00:00:00 2001 From: DROGOU Laurent Date: Sun, 24 Jan 2021 00:05:18 +0100 Subject: [PATCH] suppression du parsing html --- routes.auth.go | 35 +----- server.go | 45 ++++--- template/resultat.html | 46 -------- templateOAuth/index.go | 169 +++++++++++++++++++++++++++ {template => templateOAuth}/jwt.html | 2 +- templateOAuth/resultat.go | 69 +++++++++++ templateOAuth/resultat.html | 66 +++++++++++ 7 files changed, 341 insertions(+), 91 deletions(-) delete mode 100644 template/resultat.html create mode 100644 templateOAuth/index.go rename {template => templateOAuth}/jwt.html (99%) create mode 100644 templateOAuth/resultat.go create mode 100644 templateOAuth/resultat.html diff --git a/routes.auth.go b/routes.auth.go index d2ad6db..a8408a1 100644 --- a/routes.auth.go +++ b/routes.auth.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "encoding/base64" "encoding/json" "fmt" "html/template" @@ -10,19 +9,12 @@ import ( "net/http" "net/url" "strconv" - "strings" "time" "github.com/dgrijalva/jwt-go" + templateoauth "github.com/ldrogou/goauth20/templateOAuth" ) -//File structure du fichier -type File struct { - JwtProduce string - Header string - Payload string -} - type JsonToken struct { clientID string `json:"client_id"` clientSecret string `json:"client_secret"` @@ -53,7 +45,7 @@ func (s *server) handleIndex() http.HandlerFunc { rw.Header().Set("Content-Type", "text/html") rw.WriteHeader(http.StatusOK) - t, err := template.ParseFiles("template/jwt.html") + t, err := template.New("test").Parse(templateoauth.TemplateIndex) if err != nil { fmt.Errorf("erreur suivante %v", err) } @@ -103,25 +95,8 @@ func (s *server) handleLocal() http.HandlerFunc { rw.WriteHeader(http.StatusInternalServerError) return } - tableau := strings.Split(tokenString, ".") - header, _ := base64.URLEncoding.DecodeString(tableau[0]) - log.Println(string(string(header))) - log.Println(tableau[1]) - payload, _ := base64.URLEncoding.DecodeString(tableau[1]) - log.Println(string(payload)) - - log.Println(tableau[2]) - test, _ := base64.URLEncoding.DecodeString(tableau[2]) - log.Println(string(string(test))) - - tokenSssss := map[string]interface{}{ - "access_token": tokenString, - "header": string(header), - "payload": string(payload), - } - - s.responseFile(rw, r, tokenSssss, http.StatusOK) + s.responseFile(rw, r, tokenString, http.StatusOK) } } @@ -174,7 +149,7 @@ func (s *server) handleRedirect() http.HandlerFunc { fmt.Println("response Status:", resp.Status) fmt.Println("response Headers:", resp.Header) - var t interface{} + var t map[string]interface{} // here's the trick json.NewDecoder(resp.Body).Decode(&t) @@ -191,7 +166,7 @@ func (s *server) handleRedirect() http.HandlerFunc { return } - s.responseFile(rw, r, t, http.StatusOK) + s.responseFile(rw, r, t["access_token"], http.StatusOK) } } diff --git a/server.go b/server.go index 9ee9e99..544086f 100644 --- a/server.go +++ b/server.go @@ -6,8 +6,11 @@ import ( "html/template" "log" "net/http" + "strings" + "github.com/dgrijalva/jwt-go" "github.com/gorilla/mux" + templateoauth "github.com/ldrogou/goauth20/templateOAuth" ) type server struct { @@ -15,6 +18,14 @@ type server struct { store Store } +//File structure du fichier +type File struct { + JwtProduce string + Header string + Payload string + Sign string +} + func newServer() *server { s := &server{ router: mux.NewRouter(), @@ -42,35 +53,41 @@ func (s *server) response(rw http.ResponseWriter, _ *http.Request, data interfac } -func (s *server) responseFile(rw http.ResponseWriter, _ *http.Request, data interface{}, status int) { +func (s *server) responseFile(rw http.ResponseWriter, _ *http.Request, data interface{}, status int) error { rw.Header().Set("Content-Type", "text/html") rw.WriteHeader(status) - tokenVal := data.(interface{}).(map[string]interface{}) + tokenVal := data.(string) - //t := template.New("mon template") - tem, err := template.ParseFiles("template/resultat.html") + tableau := strings.Split(tokenVal, ".") + header, err := jwt.DecodeSegment(tableau[0]) if err != nil { - fmt.Errorf("erreur suivante %v", err) + return fmt.Errorf("Impossible de décoder le header. (err=%v)", err) + } + payload, err := jwt.DecodeSegment(tableau[1]) + if err != nil { + return fmt.Errorf("Impossible de décoder le payload. (err=%v)", err) } - sssss := tokenVal["access_token"].(string) - header := tokenVal["header"].(string) - payload := tokenVal["payload"].(string) - //sssss := "erer" - log.Println(sssss) + //t := template.New("mon template") + tem, err := template.New("Resulta").Parse(templateoauth.Resultat) + if err != nil { + return fmt.Errorf("erreur suivante %v", err) + } f := File{ - JwtProduce: sssss, - Header: header, - Payload: payload, + JwtProduce: tokenVal, + Header: string(header), + Payload: string(payload), + Sign: tableau[2], } err = tem.Execute(rw, f) if err != nil { - fmt.Errorf("erreur suivante %v", err) + return fmt.Errorf("erreur suivante %v", err) } + return nil } func (s *server) decode(rw http.ResponseWriter, r *http.Request, v interface{}) error { diff --git a/template/resultat.html b/template/resultat.html deleted file mode 100644 index da97be1..0000000 --- a/template/resultat.html +++ /dev/null @@ -1,46 +0,0 @@ - - - - - RCA JWT API - - - - - - - - - - - - -
-

Composition

-
-
-
-
- - {{.JwtProduce }} - -
-
- header -
{{.Header}}
- Payload -
- - {{.Payload}} - -
-
-
-
- - \ No newline at end of file diff --git a/templateOAuth/index.go b/templateOAuth/index.go new file mode 100644 index 0000000..22d8d91 --- /dev/null +++ b/templateOAuth/index.go @@ -0,0 +1,169 @@ +package templateoauth + +//TemplateIndex index html +var TemplateIndex = ` + + + + RCA JWT API + + + + + + + + + + + + + +
+

JWT

+
+
+
+
+
+
+ account_circle + + +
+
+
+
+ account_balance + + +
+
+
+
+ account_balance + + +
+
+
+
+ fiber_pin + + +
+
+ +
+ +
+
+
+ account_balance + + +
+
+
+
+ account_balance + + +
+
+
+
+ account_balance + + +
+
+
+
+ account_balance + + +
+
+
+
+ +
+
+ +
+
+ +
+ + + +` diff --git a/template/jwt.html b/templateOAuth/jwt.html similarity index 99% rename from template/jwt.html rename to templateOAuth/jwt.html index 6b28961..24e2063 100644 --- a/template/jwt.html +++ b/templateOAuth/jwt.html @@ -99,7 +99,7 @@
account_balance - +
diff --git a/templateOAuth/resultat.go b/templateOAuth/resultat.go new file mode 100644 index 0000000..a7745e5 --- /dev/null +++ b/templateOAuth/resultat.go @@ -0,0 +1,69 @@ +package templateoauth + +//Resultat page de resultat +var Resultat = ` + + + + RCA JWT API + + + + + + + + + + + + + +
+

Composition

+
+
+
+
+ + {{.JwtProduce }} + +
+
+ +
    +
  • +
    filter_dramaheader
    +
    +
  • +
  • +
    placepayload
    +
    +
  • +
  • +
    whatshotsignature
    +
    {{.Sign}}
    +
  • +
+
+
+
+ + + + +` diff --git a/templateOAuth/resultat.html b/templateOAuth/resultat.html new file mode 100644 index 0000000..7463501 --- /dev/null +++ b/templateOAuth/resultat.html @@ -0,0 +1,66 @@ + + + + + RCA JWT API + + + + + + + + + + + + + +
+

Composition

+
+
+
+
+ + {{.JwtProduce }} + +
+
+ +
    +
  • +
    filter_dramaheader
    +
    +
  • +
  • +
    placepayload
    +
    +
  • +
  • +
    whatshotsignature
    +
    Lorem ipsum dolor sit amet.
    +
  • +
+
+
+
+ + + + + \ No newline at end of file