ajout du champ state dans redirecturi pour récupérer les params

This commit is contained in:
2021-01-27 13:48:11 +01:00
parent e21982350b
commit cfe68a5329
2 changed files with 26 additions and 22 deletions

View File

@@ -46,12 +46,12 @@ type Claims struct {
func (s *server) handleIndex() http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
err := s.store.DeleteOauth()
err := s.store.DeleteOauth(1)
if err != nil {
fmt.Printf("erreur à la récupération des paramètres %v", err)
}
err = s.store.DeleteParam()
err = s.store.DeleteParam("state")
if err != nil {
fmt.Printf("erreur à la récupération des paramètres %v", err)
}
@@ -161,7 +161,7 @@ func (s *server) handleOAuth20() http.HandlerFunc {
rhttp := "https://" + d + "/entreprise-partenaire/authorize?client_id=" + ci +
"&scope=" + sc +
"&current_company=" + cc +
"&redirect_uri=http://localhost:8080/oauth/redirect" +
"&redirect_uri=http://localhost:8080/oauth/redirect%3Fstate=ererer" +
"&abort_uri=http://localhost:8080/index"
http.Redirect(rw, r, rhttp, http.StatusMovedPermanently)
@@ -172,12 +172,15 @@ func (s *server) handleOAuth20() http.HandlerFunc {
func (s *server) handleRedirect() http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
codes, _ := r.URL.Query()["code"]
p, err := s.store.GetParam()
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(codes[0], p)
jsonStr := constJSONToken(c, st, p)
apiURL := "https://api." + p.Domaine + "/auth/v1/oauth2.0/accessToken"
data := url.Values{}
@@ -228,8 +231,9 @@ func (s *server) handleRedirect() http.HandlerFunc {
fmt.Printf("erreur suivante %v", err)
}
monID := strconv.Itoa(int(o.ID))
// Puis redisrect vers page resultat
rj := "http://localhost:8080/jwt"
rj := "http://localhost:8080/jwt?model=" + monID
http.Redirect(rw, r, rj, http.StatusMovedPermanently)
}
}
@@ -244,7 +248,7 @@ func (s *server) handleJSONWebToken() http.HandlerFunc {
fmt.Printf("erreur suivante %v", err)
}
oauth, _ := s.store.GetOauth()
oauth, _ := s.store.GetOauth(1)
tokenVal := oauth.AccessToken
fmt.Println("============")
@@ -281,12 +285,12 @@ func (s *server) handleJSONWebToken() http.HandlerFunc {
}
}
func constJSONToken(code string, param *model.Param) JSONToken {
func constJSONToken(code, state string, param *model.Param) JSONToken {
return JSONToken{
ClientID: param.ClientID,
ClientSecret: param.ClientSecret,
GrantType: param.GrantType,
RedirectURI: "http://localhost:8080/oauth/redirect",
RedirectURI: "http://localhost:8080/oauth/redirect?state=" + state,
Code: code,
}
}

View File

@@ -12,13 +12,13 @@ type Store interface {
Open() error
Close() error
GetOauth() (*model.Oauth, error)
GetOauth(id int64) (*model.Oauth, error)
CreateOauth(m *model.Oauth) error
DeleteOauth() error
DeleteOauth(id int64) error
GetParam() (*model.Param, error)
GetParam(state string) (*model.Param, error)
CreateParam(m *model.Param) error
DeleteParam() error
DeleteParam(state string) error
}
type DbStore struct {
@@ -64,9 +64,9 @@ func (store *DbStore) Close() error {
return store.db.Close()
}
func (store *DbStore) GetOauth() (*model.Oauth, error) {
func (store *DbStore) GetOauth(id int64) (*model.Oauth, error) {
var oauth = &model.Oauth{}
err := store.db.Get(oauth, "SELECT * FROM oauth")
err := store.db.Get(oauth, "SELECT * FROM oauth where id=$1", id)
if err != nil {
return oauth, err
}
@@ -86,8 +86,8 @@ func (store *DbStore) CreateOauth(o *model.Oauth) error {
}
func (store *DbStore) DeleteOauth() error {
_, err := store.db.Exec("DELETE FROM oauth", nil)
func (store *DbStore) DeleteOauth(id int64) error {
_, err := store.db.Exec("DELETE FROM oauth where id=?", id)
if err != nil {
return err
}
@@ -95,9 +95,9 @@ func (store *DbStore) DeleteOauth() error {
return err
}
func (store *DbStore) GetParam() (*model.Param, error) {
func (store *DbStore) GetParam(state string) (*model.Param, error) {
var param = &model.Param{}
err := store.db.Get(param, "SELECT * FROM param")
err := store.db.Get(param, "SELECT * FROM param where state=$1", state)
if err != nil {
return param, err
}
@@ -116,8 +116,8 @@ func (store *DbStore) CreateParam(p *model.Param) error {
}
func (store *DbStore) DeleteParam() error {
_, err := store.db.Exec("DELETE FROM param", nil)
func (store *DbStore) DeleteParam(state string) error {
_, err := store.db.Exec("DELETE FROM param where state=?", state)
if err != nil {
return err
}