Skip to content
Snippets Groups Projects
Commit 2b8316d7 authored by Yoan VALLET's avatar Yoan VALLET
Browse files

Clean the code and remove auth_old endpoint

parent b7f851a6
Branches
No related tags found
No related merge requests found
Pipeline #5411 passed
......@@ -33,91 +33,6 @@ func main() {
mux := http.NewServeMux()
fmt.Println("Server started")
mux.HandleFunc("/auth_old", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("*******************")
fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- New auth request")
query := r.URL.Query()
fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- Query received - ", query)
clientId := query.Get("client_id")
cozyOrigin := query.Get("redirect_uri") // here we use the redirect_uri param to transmit our stack url
state := query.Get("state")
authReq := "https://gw.hml.api.enedis.fr/group/espace-particuliers/consentement-linky/oauth2/authorize?client_id="+ clientId +"&duration=P6M&redirect_uri=https://oauth-proxy.wf.alpha.grandlyon.com/&response_type=code&state="+ state +"-"+ cozyOrigin
fmt.Println(authReq)
response, err := http.Get(authReq)
if err != nil {
fmt.Println(err)
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err)
}
// Get the response body as a string
pageContent := string(contents)
// Find a substr
codeStartIndex := strings.Index(pageContent, "?code=")
if codeStartIndex == -1 {
fmt.Println("No code found")
}
codeStartIndex += 6
// Find the index of the closing tag
codeEndIndex := strings.Index(pageContent, "&state=")
if codeEndIndex == -1 {
fmt.Println("No closing tag for code found.")
}
stateStartIndex := strings.Index(pageContent, "&state=")
if stateStartIndex == -1 {
fmt.Println("No state found")
}
stateStartIndex += 7
stateEndIndex := strings.Index(pageContent, "&usage_point_id=")
if stateEndIndex == -1 {
fmt.Println("No closing tag for state found.")
}
usageStartIndex := strings.Index(pageContent, "&usage_point_id=")
if usageStartIndex == -1 {
fmt.Println("No usage found")
}
usageStartIndex += 16
usageEndIndex := strings.Index(pageContent, "&usage_point_id=")
if usageEndIndex == -1 {
fmt.Println("No closing tag for usage found.")
}
usageEndIndex += 30
pageCode := string([]byte(pageContent[codeStartIndex:codeEndIndex]))
pageState := string([]byte(pageContent[stateStartIndex:stateEndIndex]))
pageUsage := string([]byte(pageContent[usageStartIndex:usageEndIndex]))
// Print out the result
fmt.Printf("Page code: %s\n", pageCode)
fmt.Printf("Page state: %s\n", pageState)
fmt.Printf("Page usage: %s\n", pageUsage)
splitIndex := strings.Index(pageState, "-")
if splitIndex == -1 {
fmt.Println("No host found")
}
state := string([]byte(pageState[0:splitIndex]))
encodedHost := string([]byte(pageState[splitIndex+1:len(pageState)]))
host, err := url.QueryUnescape(encodedHost)
if err != nil {
fmt.Println("Host cannot be decoded")
}
redir := host + "?code=" + pageCode + "&state="+ state +"&usage_point_id=" + pageUsage
fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- Redirect to -", redir)
http.Redirect(w, r, redir, 302)
}
})
mux.HandleFunc("/auth", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("*******************")
fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- New auth request")
......@@ -127,7 +42,7 @@ func main() {
clientId := query.Get("client_id")
state := query.Get("state")
cozyOrigin := query.Get("redirect_uri") // here we use the redirect_uri param to transmit our stack url
redirectUri := "https://oauth-proxy.wf.alpha.grandlyon.com/redirect"
redirectUri := "https://oauth-proxy.wf.alpha.grandlyon.com/"
responseType := "code"
authReq := "https://gw.hml.api.enedis.fr/group/espace-particuliers/consentement-linky/oauth2/authorize?client_id="+ clientId +"&duration=P6M&redirect_uri="+ redirectUri +"&response_type="+ responseType +"&state="+ state +"-"+ cozyOrigin
......@@ -143,21 +58,16 @@ func main() {
if err != nil {
fmt.Println(err)
}
// Define the replyUri
// replyUri := "https://oauth-proxy.wf.alpha.grandlyon.com/redirect"
// fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- Reply to - ", replyUri)
// TO BE REMOVE FOR PROD API
// Get the response body as a string
pageContentString := string(contents)
// Replace redirectUri by the host in the body
// pageContentString = strings.ReplaceAll(pageContentString, redirectUri, replyUri)
// Prevent the closure of the opener
pageContentString = strings.ReplaceAll(pageContentString, "this.window.opener.location.href = url;", "this.window.location.href = url;")
fmt.Println(pageContentString)
// fmt.Println(pageContentString)
// Convert string to byte for response writting
newPageContentByte := []byte(pageContentString)
// fmt.Println(r.Host)
if response.StatusCode >= 200 && response.StatusCode <= 299 {
w.Write(newPageContentByte)
} else {
......@@ -166,7 +76,8 @@ func main() {
}
})
mux.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) {
// To be change by "/redirect" once enedis will have change the redirect uri
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("*******************")
fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- New redirect request")
query := r.URL.Query()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment