Skip to content
Snippets Groups Projects
Commit 487a60da authored by Sébastien Blaisot's avatar Sébastien Blaisot
Browse files

gofmt

parent 891b08b0
No related branches found
No related tags found
1 merge request!1Cozy updates
package main package main
import ( import (
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"time" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"io/ioutil"
"strconv" "strconv"
"strings" "strings"
"encoding/json" "time"
) )
var ( var (
...@@ -17,21 +17,21 @@ var ( ...@@ -17,21 +17,21 @@ var (
) )
type TokenResponse struct { type TokenResponse struct {
AccessToken string `json:"access_token"` AccessToken string `json:"access_token"`
TokenType string `json:"token_type"` TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"` ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"` RefreshToken string `json:"refresh_token"`
Scope string `json:"scope"` Scope string `json:"scope"`
RefreshTokenIssuedAt string `json:"refresh_token_issued_at"` RefreshTokenIssuedAt string `json:"refresh_token_issued_at"`
IssueAt string `json:"issued_at"` IssueAt string `json:"issued_at"`
UsagePointId string `json:"usage_points_id"` UsagePointId string `json:"usage_points_id"`
} }
func main() { func main() {
// Parse the flags // Parse the flags
flag.Parse() flag.Parse()
mux := http.NewServeMux() mux := http.NewServeMux()
fmt.Println("Server started") fmt.Println("Server started")
mux.HandleFunc("/auth", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/auth", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("*******************") fmt.Println("*******************")
...@@ -43,14 +43,14 @@ func main() { ...@@ -43,14 +43,14 @@ func main() {
state := query.Get("state") state := query.Get("state")
cozyOrigin := query.Get("redirect_uri") // here we use the redirect_uri param to transmit our stack url 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/redirect"
responseType := "code" responseType := "code"
// DEV API // DEV API
// authURL := "https://gw.hml.api.enedis.fr/dataconnect/v1/oauth2/authorize" // authURL := "https://gw.hml.api.enedis.fr/dataconnect/v1/oauth2/authorize"
// PROD API // PROD API
authURL := "https://mon-compte-particulier.enedis.fr/dataconnect/v1/oauth2/authorize" authURL := "https://mon-compte-particulier.enedis.fr/dataconnect/v1/oauth2/authorize"
redirectUrl := authURL +"?client_id="+ clientId +"&duration=P6M&redirect_uri="+ redirectUri +"&response_type="+ responseType +"&state="+ state +"-"+ cozyOrigin redirectUrl := authURL + "?client_id=" + clientId + "&duration=P6M&redirect_uri=" + redirectUri + "&response_type=" + responseType + "&state=" + state + "-" + cozyOrigin
fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- Redirect to - ", redirectUrl) fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- Redirect to - ", redirectUrl)
http.Redirect(w, r, redirectUrl, 302) http.Redirect(w, r, redirectUrl, 302)
...@@ -70,18 +70,18 @@ func main() { ...@@ -70,18 +70,18 @@ func main() {
fmt.Println("No host found") fmt.Println("No host found")
} }
state := req_state[0:splitIndex] state := req_state[0:splitIndex]
host := req_state[splitIndex+1:len(req_state)] host := req_state[splitIndex+1:]
usagePointId := query.Get("usage_point_id") usagePointId := query.Get("usage_point_id")
redir := host + "?code=" + code + "&state="+ state +"&usage_point_id=" + usagePointId redir := host + "?code=" + code + "&state=" + state + "&usage_point_id=" + usagePointId
fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- Redirect to -", redir) fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- Redirect to -", redir)
http.Redirect(w, r, redir, 302) http.Redirect(w, r, redir, 302)
}) })
mux.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("*******************") fmt.Println("*******************")
fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- New token request") fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- New token request")
query := r.URL.Query() query := r.URL.Query()
fmt.Println(query) fmt.Println(query)
...@@ -106,7 +106,7 @@ func main() { ...@@ -106,7 +106,7 @@ func main() {
http.Error(w, http.StatusText(500), 500) http.Error(w, http.StatusText(500), 500)
} }
clientIdStartIndex += 10 clientIdStartIndex += 10
clientId = pageContent[clientIdStartIndex:clientIdStartIndex + 36] clientId = pageContent[clientIdStartIndex : clientIdStartIndex+36]
//Check for client_secret //Check for client_secret
clientSecretStartIndex := strings.Index(pageContent, "client_secret=") clientSecretStartIndex := strings.Index(pageContent, "client_secret=")
if clientSecretStartIndex == -1 { if clientSecretStartIndex == -1 {
...@@ -114,14 +114,14 @@ func main() { ...@@ -114,14 +114,14 @@ func main() {
http.Error(w, http.StatusText(500), 500) http.Error(w, http.StatusText(500), 500)
} }
clientSecretStartIndex += 14 clientSecretStartIndex += 14
clientSecret = pageContent[clientSecretStartIndex:clientSecretStartIndex + 36] clientSecret = pageContent[clientSecretStartIndex : clientSecretStartIndex+36]
//Check for code //Check for code
codeStartIndex := strings.Index(pageContent, "code=") codeStartIndex := strings.Index(pageContent, "code=")
if codeStartIndex == -1 { if codeStartIndex == -1 {
fmt.Println(time.Now().Format("2006-01-02 15:04:05"), " - Info - No code found (optionnal param)") fmt.Println(time.Now().Format("2006-01-02 15:04:05"), " - Info - No code found (optionnal param)")
} else { } else {
codeStartIndex += 5 codeStartIndex += 5
code = pageContent[codeStartIndex:codeStartIndex + 30] code = pageContent[codeStartIndex : codeStartIndex+30]
} }
//Check for grant_type //Check for grant_type
grandTypeStartIndex := strings.Index(pageContent, "grant_type=") grandTypeStartIndex := strings.Index(pageContent, "grant_type=")
...@@ -130,7 +130,7 @@ func main() { ...@@ -130,7 +130,7 @@ func main() {
http.Error(w, http.StatusText(500), 500) http.Error(w, http.StatusText(500), 500)
} }
grandTypeStartIndex += 11 grandTypeStartIndex += 11
tempGrandTypeString := pageContent[grandTypeStartIndex:len(pageContent)] tempGrandTypeString := pageContent[grandTypeStartIndex:]
grandTypeEndIndex := strings.Index(tempGrandTypeString, "&") grandTypeEndIndex := strings.Index(tempGrandTypeString, "&")
if grandTypeEndIndex == -1 { if grandTypeEndIndex == -1 {
fmt.Println(time.Now().Format("2006-01-02 15:04:05"), " - Error - No closing tag for grant_type found") fmt.Println(time.Now().Format("2006-01-02 15:04:05"), " - Error - No closing tag for grant_type found")
...@@ -144,7 +144,7 @@ func main() { ...@@ -144,7 +144,7 @@ func main() {
http.Error(w, http.StatusText(500), 500) http.Error(w, http.StatusText(500), 500)
} }
refershTokenStartIndex += 14 refershTokenStartIndex += 14
refreshToken = pageContent[refershTokenStartIndex:refershTokenStartIndex + 46] refreshToken = pageContent[refershTokenStartIndex : refershTokenStartIndex+46]
} else { } else {
// Retrieve params from query // Retrieve params from query
clientId = query.Get("client_id") clientId = query.Get("client_id")
...@@ -159,12 +159,12 @@ func main() { ...@@ -159,12 +159,12 @@ func main() {
fmt.Printf("code: %s\n", code) fmt.Printf("code: %s\n", code)
fmt.Printf("grant_type: %s\n", grantType) fmt.Printf("grant_type: %s\n", grantType)
fmt.Printf("refresh_token: %s\n", refreshToken) fmt.Printf("refresh_token: %s\n", refreshToken)
// DEV API // DEV API
// tokenUrl := "https://gw.hml.api.enedis.fr/v1/oauth2/token" // tokenUrl := "https://gw.hml.api.enedis.fr/v1/oauth2/token"
// PROD API // PROD API
tokenUrl := "https://gw.prd.api.enedis.fr/v1/oauth2/token" tokenUrl := "https://gw.prd.api.enedis.fr/v1/oauth2/token"
data := url.Values{} data := url.Values{}
data.Set("client_id", clientId) data.Set("client_id", clientId)
data.Set("client_secret", clientSecret) data.Set("client_secret", clientSecret)
...@@ -193,7 +193,7 @@ func main() { ...@@ -193,7 +193,7 @@ func main() {
http.Error(w, decodeError.Error(), 500) http.Error(w, decodeError.Error(), 500)
return return
} }
// Response with json data // Response with json data
jsonError := json.NewEncoder(w).Encode(data) jsonError := json.NewEncoder(w).Encode(data)
if jsonError != nil { if jsonError != nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment