Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
cozy_oauth_proxy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
web-et-numerique
Factory
LLLE_Project
cozy_oauth_proxy
Commits
f775ab7b
Commit
f775ab7b
authored
5 years ago
by
Yoan VALLET
Browse files
Options
Downloads
Patches
Plain Diff
Update main.go - add redirecte endpoint
parent
fae89d21
Branches
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#5388
passed
5 years ago
Stage: build
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
main.go
+115
-1
115 additions, 1 deletion
main.go
with
115 additions
and
1 deletion
main.go
+
115
−
1
View file @
f775ab7b
...
@@ -33,7 +33,7 @@ func main() {
...
@@ -33,7 +33,7 @@ func main() {
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
_old
"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
fmt
.
Println
(
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
),
"- New auth request"
)
fmt
.
Println
(
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
),
"- New auth request"
)
query
:=
r
.
URL
.
Query
()
query
:=
r
.
URL
.
Query
()
fmt
.
Println
(
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
),
"- Query received - "
,
query
)
fmt
.
Println
(
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
),
"- Query received - "
,
query
)
...
@@ -62,6 +62,89 @@ func main() {
...
@@ -62,6 +62,89 @@ func main() {
replyUri
=
replyUri
+
"accounts/enedis-konnector/redirect"
replyUri
=
replyUri
+
"accounts/enedis-konnector/redirect"
fmt
.
Println
(
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
),
"- Reply to - "
,
replyUri
)
fmt
.
Println
(
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
),
"- Reply to - "
,
replyUri
)
// 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
)
redir
:=
replyUri
+
"?code="
+
pageCode
+
"&state="
+
pageState
+
"&usage_point_id="
+
pageUsage
fmt
.
Println
(
redir
)
http
.
Redirect
(
w
,
r
,
redir
,
302
)
}
})
mux
.
HandleFunc
(
"/auth"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
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"
)
state
:=
query
.
Get
(
"state"
)
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
fmt
.
Println
(
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
),
"- Send request to auth endpoint"
,
authReq
)
response
,
err
:=
http
.
Get
(
authReq
)
fmt
.
Println
(
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
),
"- Endpoint response with status"
,
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
)
}
// Define the replyUri
replyUri
:=
"oauth-proxy.wf.alpha.grandlyon.com/redirect"
fmt
.
Println
(
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
),
"- Reply to - "
,
replyUri
)
// Get the response body as a string
// Get the response body as a string
pageContentString
:=
string
(
contents
)
pageContentString
:=
string
(
contents
)
// Replace redirectUri by the host in the body
// Replace redirectUri by the host in the body
...
@@ -79,6 +162,37 @@ func main() {
...
@@ -79,6 +162,37 @@ func main() {
}
}
})
})
mux
.
HandleFunc
(
"/redirect"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
fmt
.
Println
(
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
),
"- New redirect request"
)
query
:=
r
.
URL
.
Query
()
fmt
.
Println
(
query
)
code
:=
query
.
Get
(
"code"
)
state
:=
query
.
Get
(
"state"
)
usagePointId
:=
query
.
Get
(
"usage_point_id"
)
// Get the redirect uri from the referer
referer
:=
r
.
Header
.
Get
(
"Referer"
)
redirectUri
:=
""
redirectStartIndex
:=
strings
.
Index
(
referer
,
"&redirect_uri="
)
if
redirectStartIndex
==
-
1
{
fmt
.
Println
(
"No redirect found"
)
}
redirectStartIndex
+=
len
(
"&redirect_uri="
)
splitRefer
:=
string
([]
byte
(
referer
[
redirectStartIndex
:
len
(
referer
)]))
fmt
.
Println
(
splitRefer
)
redirectEndIndex
:=
strings
.
Index
(
splitRefer
,
"&"
)
if
redirectEndIndex
==
-
1
{
redirectUri
=
splitRefer
}
redirectUri
=
string
([]
byte
(
splitRefer
[
0
:
redirectEndIndex
]))
fmt
.
Println
(
redirectUri
)
redir
:=
"https://"
+
redirectUri
+
"/accounts/enedis-konnector/redirect?code="
+
code
+
"&state="
+
state
+
"&usage_point_id="
+
usagePointId
fmt
.
Println
(
redir
)
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
(
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
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment