diff --git a/main.go b/main.go index 54daa0d..13657f5 100644 --- a/main.go +++ b/main.go @@ -30,9 +30,9 @@ func run() error { http.HandleFunc("/", srv.serveHTTP) - port := 8080 + port := 8090 log.Printf("servering http port %v", port) - err = http.ListenAndServe(":8080", nil) + err = http.ListenAndServe(":8090", nil) if err != nil { return err } diff --git a/routes.auth.go b/routes.auth.go index eb2af41..0df506d 100644 --- a/routes.auth.go +++ b/routes.auth.go @@ -40,6 +40,7 @@ type Claims struct { Sub string `json:"sub"` IDEntreprise string `json:"idEntreprise"` RcaPartnerID string `json:"rcaPartnerId"` + Scopes []string `json:"scopes"` Roles []string `json:"roles"` jwt.StandardClaims } @@ -68,32 +69,36 @@ func (s *server) handleLocal() http.HandlerFunc { sub := r.FormValue("sub") idEntreprise := r.FormValue("id_entreprise") rcaPartnerID := r.FormValue("rcaPartnerId") - var jwtKey = []byte(r.FormValue("secret")) + jwtKey := r.FormValue("secret") + scopes := r.FormValue("scopes") + roles := r.FormValue("roles") + + var sc []string + sc = append(sc, scopes) + + rs := strings.Fields(roles) // Declare the expiration time of the token // here, we have kept it as 5 minutes expirationTime := time.Now().Add(5 * time.Hour) - roles := []string{"RCA_CLOUD_EXPERT_COMPTABLE", - "E_COLLECTE_BO_CREA", - "E_CREATION_CREA", - "E_QUESTIONNAIRE_CREA"} // Create the JWT claims, which includes the username and expiry time claims := &Claims{ Sub: sub, IDEntreprise: idEntreprise, RcaPartnerID: rcaPartnerID, - Roles: roles, + Roles: rs, + Scopes: sc, StandardClaims: jwt.StandardClaims{ // In JWT, the expiry time is expressed as unix milliseconds ExpiresAt: expirationTime.Unix(), }, } + secretBase64, err := jwt.DecodeSegment(jwtKey) // Declare the token with the algorithm used for signing, and the claims - ts := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) - + ts := jwt.NewWithClaims(jwt.SigningMethodHS512, claims) + at, err := ts.SignedString(secretBase64) // Create the JWT string - at, err := ts.SignedString(jwtKey) if err != nil { log.Printf("erreur %v", err) // If there is an error in creating the JWT return an internal server error @@ -116,7 +121,7 @@ func (s *server) handleLocal() http.HandlerFunc { monID := strconv.Itoa(int(o.ID)) // Puis redisrect vers page resultat - rj := "http://localhost:8080/jwt?model=" + monID + rj := "http://localhost:8090/jwt?model=" + monID http.Redirect(rw, r, rj, http.StatusMovedPermanently) } @@ -163,8 +168,8 @@ func (s *server) handleOAuth20() http.HandlerFunc { rhttp := "https://" + d + "/entreprise-partenaire/authorize?client_id=" + ci + "&scope=" + sc + "¤t_company=" + cc + - "&redirect_uri=http://localhost:8080/oauth/redirect%3Fstate=" + st + - "&abort_uri=http://localhost:8080/index" + "&redirect_uri=http://localhost:8090/oauth/redirect%3Fstate=" + st + + "&abort_uri=http://localhost:8090/index" http.Redirect(rw, r, rhttp, http.StatusMovedPermanently) } @@ -239,7 +244,7 @@ func (s *server) handleRedirect() http.HandlerFunc { monID := strconv.Itoa(int(o.ID)) // Puis redisrect vers page resultat - rj := "http://localhost:8080/jwt?model=" + monID + rj := "http://localhost:8090/jwt?model=" + monID http.Redirect(rw, r, rj, http.StatusMovedPermanently) } } @@ -300,7 +305,7 @@ func constJSONToken(code, state string, param *model.Param) JSONToken { ClientID: param.ClientID, ClientSecret: param.ClientSecret, GrantType: param.GrantType, - RedirectURI: "http://localhost:8080/oauth/redirect%3Fstate=" + state, + RedirectURI: "http://localhost:8090/oauth/redirect%3Fstate=" + state, Code: code, } } diff --git a/templateOAuth/index.go b/templateOAuth/index.go index 22d8d91..82f4cf3 100644 --- a/templateOAuth/index.go +++ b/templateOAuth/index.go @@ -16,64 +16,12 @@ var TemplateIndex = ` @@ -87,21 +35,35 @@ var TemplateIndex = `