Skip to content
Snippets Groups Projects
main.go 6.95 KiB
Newer Older
  • Learn to ignore specific revisions
  • Nicolas PERNOUD's avatar
    Nicolas PERNOUD committed
    package main
    
    import (
    	"flag"
    
    Nicolas PERNOUD's avatar
    Nicolas PERNOUD committed
    	"net/http"
    
    	"net/url"
    
    Nicolas PERNOUD's avatar
    Nicolas PERNOUD committed
    	"strconv"
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    	"strings"
    
    Nicolas PERNOUD's avatar
    Nicolas PERNOUD committed
    )
    
    var (
    	httpPort = flag.Int("http_port", 80, "HTTP port to serve on (defaults to 80)")
    )
    
    func main() {
    	// Parse the flags
    	flag.Parse()
    	mux := http.NewServeMux()
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
        fmt.Println("Server started")
    
    	mux.HandleFunc("/auth", func(w http.ResponseWriter, r *http.Request) {
    
            fmt.Println("New auth request")
    
    Nicolas PERNOUD's avatar
    Nicolas PERNOUD committed
    		query := r.URL.Query()
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    		fmt.Println(query)
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    		// host := strings.Split(query.Get("state"), "-")[1]
    
    		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
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    		fmt.Println(authReq)
    		response, err := http.Get(authReq)
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    			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
    
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    			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)
    
    			state := strings.Split(pageState, "-")[0]
    			host := strings.Split(pageState, "-")[1]
    
    			redir := "https://" + host + "/accounts/enedisoauth/redirect?code=" + pageCode + "&state="+ state +"&usage_point_id=" + pageUsage
    			fmt.Println(redir)
    			http.Redirect(w, r, redir, 302)
    
    	})
    
    	mux.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
            fmt.Println("New token request")
    		query := r.URL.Query()
    		fmt.Println(query)
    
    		clientId := query.Get("client_id")
    		clientSecret := query.Get("client_secret")
    		code := query.Get("code")
    		grantType := query.Get("grant_type")
    
    		// redirectUri := query.Get("redirect_uri")
    
    
    		tokenUrl := "https://gw.hml.api.enedis.fr/v1/oauth2/token"
    		data := url.Values{}
    		data.Set("client_id", clientId)
    		data.Set("client_secret", clientSecret)
    		data.Set("code", code)
    		data.Set("grant_type", grantType)
    		
    		client := &http.Client{}
    
    		req, _ := http.NewRequest("POST", tokenUrl, strings.NewReader(data.Encode()))
    		req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
    
    		response, err := client.Do(req)
    
    		fmt.Println(response.Status)
    		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
    			fmt.Printf("Response content: %s\n", string(contents))
    		
    
    			// 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)
    
    			// state := strings.Split(pageState, "-")[0]
    			// host := strings.Split(pageState, "-")[1]
    
    			// redir := "https://" + host + "/accounts/enedisoauth/redirect?code=" + pageCode + "&state="+ state +"&usage_point_id=" + pageUsage
    			// fmt.Println(redir)
    			// http.Redirect(w, r, redir, 302)
    		}
    
    	mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
    
    		query := r.URL.Query()
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    		fmt.Println(query)
    
    		clientId := query.Get("client_id")
    		cozyOrigin := "hugo.pchugo.wf.alpha.grandlyon.com"
    		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)
    			}
    			pageContent := string(contents)
    			codeStartIndex := strings.Index(pageContent, "?code=")
    			if codeStartIndex == -1 {
    				fmt.Println("No code found")
    			}
    			codeStartIndex += 6
    			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]))
    			fmt.Printf("Page code: %s\n", pageCode)
    			fmt.Printf("Page state: %s\n", pageState)
    			fmt.Printf("Page usage: %s\n", pageUsage)
    			state := strings.Split(pageState, "-")[0]
    			host := strings.Split(pageState, "-")[1]
    			redir := "https://" + host + "/accounts/enedisoauth/redirect?code=" + pageCode + "&state="+ state +"&usage_point_id=" + pageUsage
    			fmt.Println(redir)
    			http.Redirect(w, r, redir, 302)
    		}
    
    Nicolas PERNOUD's avatar
    Nicolas PERNOUD committed
    	})
    
    	http.ListenAndServe(":"+strconv.Itoa(*httpPort), mux)
    }