séparation des routes
This commit is contained in:
159
routes.auth.go
159
routes.auth.go
@@ -1,14 +1,11 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -18,23 +15,6 @@ import (
|
|||||||
templateoauth "github.com/ldrogou/goauth20/templateOAuth"
|
templateoauth "github.com/ldrogou/goauth20/templateOAuth"
|
||||||
)
|
)
|
||||||
|
|
||||||
//JSONToken json token
|
|
||||||
type JSONToken struct {
|
|
||||||
ClientID string `json:"client_id"`
|
|
||||||
ClientSecret string `json:"client_secret"`
|
|
||||||
GrantType string `json:"grant_type"`
|
|
||||||
RedirectURI string `json:"redirect_uri"`
|
|
||||||
Code string `json:"code"`
|
|
||||||
}
|
|
||||||
|
|
||||||
//Token token
|
|
||||||
type Token struct {
|
|
||||||
AccessToken string `json:"access_token"`
|
|
||||||
TokenType string `json:"token_type"`
|
|
||||||
ExpiresIn int `json:"expires_in"`
|
|
||||||
RefreshToken string `json:"refresh_token"`
|
|
||||||
}
|
|
||||||
|
|
||||||
//Claim claims to export
|
//Claim claims to export
|
||||||
type Claims struct {
|
type Claims struct {
|
||||||
Sub string `json:"sub"`
|
Sub string `json:"sub"`
|
||||||
@@ -176,142 +156,3 @@ func (s *server) handleOAuth20() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) handleRedirect() http.HandlerFunc {
|
|
||||||
return func(rw http.ResponseWriter, r *http.Request) {
|
|
||||||
|
|
||||||
c := r.URL.Query().Get("code")
|
|
||||||
st := r.URL.Query().Get("state")
|
|
||||||
|
|
||||||
// ici jouter la récupération du param
|
|
||||||
p, err := s.store.GetParam(st)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("erreur à la recupération des param (err=%v)", err)
|
|
||||||
}
|
|
||||||
jsonStr := constJSONToken(c, st, p)
|
|
||||||
//log.Printf("jsonStr %v", jsonStr)
|
|
||||||
apiURL := "https://api." + p.Domaine + "/auth/v1/oauth2.0/accessToken"
|
|
||||||
data := url.Values{}
|
|
||||||
log.Printf("data %v", data)
|
|
||||||
data.Set("client_id", jsonStr.ClientID)
|
|
||||||
data.Set("client_secret", jsonStr.ClientSecret)
|
|
||||||
//"YNVZF88dD4vny59k")
|
|
||||||
data.Set("grant_type", jsonStr.GrantType)
|
|
||||||
data.Set("redirect_uri", jsonStr.RedirectURI)
|
|
||||||
data.Set("code", jsonStr.Code)
|
|
||||||
|
|
||||||
client := &http.Client{}
|
|
||||||
req, err := http.NewRequest("POST", apiURL, bytes.NewBufferString(data.Encode()))
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("erreur sur le post (err=%v)", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
||||||
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
|
|
||||||
req.Header.Add("Accept", "application/json")
|
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("client erreur %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if resp.StatusCode != 200 {
|
|
||||||
log.Printf("Problème dans la requete retour http %v", resp.StatusCode)
|
|
||||||
s.response(rw, r, nil, http.StatusBadGateway)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var t map[string]interface{}
|
|
||||||
// here's the trick
|
|
||||||
err = json.NewDecoder(resp.Body).Decode(&t)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Cannot parse token body err=%v", err)
|
|
||||||
s.response(rw, r, nil, http.StatusBadGateway)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
// Insert en base de données
|
|
||||||
o := &model.Oauth{
|
|
||||||
ID: 0,
|
|
||||||
AccessToken: t["access_token"].(string),
|
|
||||||
TokenType: t["token_type"].(string),
|
|
||||||
ExpiresIN: t["expires_in"].(float64),
|
|
||||||
RefreshToken: t["refresh_token"].(string),
|
|
||||||
}
|
|
||||||
err = s.store.CreateOauth(o)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("erreur suivante %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
monID := strconv.Itoa(int(o.ID))
|
|
||||||
// Puis redisrect vers page resultat
|
|
||||||
rj := "http://localhost:8090/jwt?model=" + monID
|
|
||||||
http.Redirect(rw, r, rj, http.StatusMovedPermanently)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *server) handleRefreshToken() http.HandlerFunc {
|
|
||||||
return func(rw http.ResponseWriter, r *http.Request) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *server) handleJSONWebToken() http.HandlerFunc {
|
|
||||||
return func(rw http.ResponseWriter, r *http.Request) {
|
|
||||||
|
|
||||||
c := r.URL.Query().Get("model")
|
|
||||||
//c, _ := mux.Vars(r)["model"]
|
|
||||||
rw.Header().Set("Content-Type", "text/html")
|
|
||||||
rw.WriteHeader(http.StatusOK)
|
|
||||||
|
|
||||||
t, err := template.New("test").Parse(templateoauth.TemplateIndex)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("erreur suivante %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
oauthID, err := strconv.ParseInt(c, 10, 64)
|
|
||||||
|
|
||||||
oauth, err := s.store.GetOauth(oauthID)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("erreur a la récupération oauth (err=%v)", err)
|
|
||||||
}
|
|
||||||
tokenVal := oauth.AccessToken
|
|
||||||
|
|
||||||
tableau := strings.Split(tokenVal, ".")
|
|
||||||
header, err := jwt.DecodeSegment(tableau[0])
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Impossible de décoder le header. (err=%v)", err)
|
|
||||||
}
|
|
||||||
payload, err := jwt.DecodeSegment(tableau[1])
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Impossible de décoder le payload. (err=%v)", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
//t := template.New("mon template")
|
|
||||||
t, err = template.New("Resultat").Parse(templateoauth.Resultat)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("erreur suivante %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
f := File{
|
|
||||||
JwtProduce: tokenVal,
|
|
||||||
Header: string(header),
|
|
||||||
Payload: string(payload),
|
|
||||||
Sign: tableau[2],
|
|
||||||
}
|
|
||||||
|
|
||||||
err = t.Execute(rw, f)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("erreur suivante %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func constJSONToken(code, state string, param *model.Param) JSONToken {
|
|
||||||
return JSONToken{
|
|
||||||
ClientID: param.ClientID,
|
|
||||||
ClientSecret: param.ClientSecret,
|
|
||||||
GrantType: param.GrantType,
|
|
||||||
RedirectURI: "http://localhost:8090/oauth/redirect%3Fstate=" + state,
|
|
||||||
Code: code,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
161
routes.jwt.go
Normal file
161
routes.jwt.go
Normal file
@@ -0,0 +1,161 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"html/template"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/dgrijalva/jwt-go"
|
||||||
|
"github.com/ldrogou/goauth20/model"
|
||||||
|
templateoauth "github.com/ldrogou/goauth20/templateOAuth"
|
||||||
|
)
|
||||||
|
|
||||||
|
//JSONToken json token
|
||||||
|
type JSONToken struct {
|
||||||
|
ClientID string `json:"client_id"`
|
||||||
|
ClientSecret string `json:"client_secret"`
|
||||||
|
GrantType string `json:"grant_type"`
|
||||||
|
RedirectURI string `json:"redirect_uri"`
|
||||||
|
Code string `json:"code"`
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (s *server) handleRedirect() http.HandlerFunc {
|
||||||
|
return func(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
c := r.URL.Query().Get("code")
|
||||||
|
st := r.URL.Query().Get("state")
|
||||||
|
|
||||||
|
// ici jouter la récupération du param
|
||||||
|
p, err := s.store.GetParam(st)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("erreur à la recupération des param (err=%v)", err)
|
||||||
|
}
|
||||||
|
jsonStr := constJSONToken(c, st, p)
|
||||||
|
//log.Printf("jsonStr %v", jsonStr)
|
||||||
|
apiURL := "https://api." + p.Domaine + "/auth/v1/oauth2.0/accessToken"
|
||||||
|
data := url.Values{}
|
||||||
|
log.Printf("data %v", data)
|
||||||
|
data.Set("client_id", jsonStr.ClientID)
|
||||||
|
data.Set("client_secret", jsonStr.ClientSecret)
|
||||||
|
//"YNVZF88dD4vny59k")
|
||||||
|
data.Set("grant_type", jsonStr.GrantType)
|
||||||
|
data.Set("redirect_uri", jsonStr.RedirectURI)
|
||||||
|
data.Set("code", jsonStr.Code)
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
req, err := http.NewRequest("POST", apiURL, bytes.NewBufferString(data.Encode()))
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("erreur sur le post (err=%v)", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
|
||||||
|
req.Header.Add("Accept", "application/json")
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("client erreur %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
log.Printf("Problème dans la requete retour http %v", resp.StatusCode)
|
||||||
|
s.response(rw, r, nil, http.StatusBadGateway)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var t map[string]interface{}
|
||||||
|
// here's the trick
|
||||||
|
err = json.NewDecoder(resp.Body).Decode(&t)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Cannot parse token body err=%v", err)
|
||||||
|
s.response(rw, r, nil, http.StatusBadGateway)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// Insert en base de données
|
||||||
|
o := &model.Oauth{
|
||||||
|
ID: 0,
|
||||||
|
AccessToken: t["access_token"].(string),
|
||||||
|
TokenType: t["token_type"].(string),
|
||||||
|
ExpiresIN: t["expires_in"].(float64),
|
||||||
|
RefreshToken: t["refresh_token"].(string),
|
||||||
|
}
|
||||||
|
err = s.store.CreateOauth(o)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("erreur suivante %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
monID := strconv.Itoa(int(o.ID))
|
||||||
|
// Puis redisrect vers page resultat
|
||||||
|
rj := "http://localhost:8090/jwt?model=" + monID
|
||||||
|
http.Redirect(rw, r, rj, http.StatusMovedPermanently)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) handleJSONWebToken() http.HandlerFunc {
|
||||||
|
return func(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
c := r.URL.Query().Get("model")
|
||||||
|
|
||||||
|
rw.Header().Set("Content-Type", "text/html")
|
||||||
|
rw.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
|
t, err := template.New("test").Parse(templateoauth.TemplateIndex)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("erreur suivante %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
oauthID, err := strconv.ParseInt(c, 10, 64)
|
||||||
|
|
||||||
|
oauth, err := s.store.GetOauth(oauthID)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("erreur a la récupération oauth (err=%v)", err)
|
||||||
|
}
|
||||||
|
tokenVal := oauth.AccessToken
|
||||||
|
|
||||||
|
tableau := strings.Split(tokenVal, ".")
|
||||||
|
header, err := jwt.DecodeSegment(tableau[0])
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Impossible de décoder le header. (err=%v)", err)
|
||||||
|
}
|
||||||
|
payload, err := jwt.DecodeSegment(tableau[1])
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Impossible de décoder le payload. (err=%v)", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
//t := template.New("mon template")
|
||||||
|
t, err = template.New("Resultat").Parse(templateoauth.Resultat)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("erreur suivante %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
f := File{
|
||||||
|
JwtProduce: tokenVal,
|
||||||
|
Header: string(header),
|
||||||
|
Payload: string(payload),
|
||||||
|
Sign: tableau[2],
|
||||||
|
}
|
||||||
|
|
||||||
|
err = t.Execute(rw, f)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("erreur suivante %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func constJSONToken(code, state string, param *model.Param) JSONToken {
|
||||||
|
return JSONToken{
|
||||||
|
ClientID: param.ClientID,
|
||||||
|
ClientSecret: param.ClientSecret,
|
||||||
|
GrantType: param.GrantType,
|
||||||
|
RedirectURI: "http://localhost:8090/oauth/redirect%3Fstate=" + state,
|
||||||
|
Code: code,
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user