Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
8c779729
authored
Sep 15, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: fix oauth2 state not checking
parent
23de4f73
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
58 additions
and
18 deletions
+58
-18
controller/github.go
+27
-0
router/api-router.go
+1
-0
web/src/components/GitHubOAuth.js
+5
-4
web/src/components/LoginForm.js
+2
-7
web/src/components/PersonalSetting.js
+2
-7
web/src/components/utils.js
+21
-0
No files found.
controller/github.go
View file @
8c779729
...
@@ -79,6 +79,14 @@ func getGitHubUserInfoByCode(code string) (*GitHubUser, error) {
...
@@ -79,6 +79,14 @@ func getGitHubUserInfoByCode(code string) (*GitHubUser, error) {
func
GitHubOAuth
(
c
*
gin
.
Context
)
{
func
GitHubOAuth
(
c
*
gin
.
Context
)
{
session
:=
sessions
.
Default
(
c
)
session
:=
sessions
.
Default
(
c
)
state
:=
c
.
Query
(
"state"
)
if
state
==
""
||
session
.
Get
(
"oauth_state"
)
==
nil
||
state
!=
session
.
Get
(
"oauth_state"
)
.
(
string
)
{
c
.
JSON
(
http
.
StatusForbidden
,
gin
.
H
{
"success"
:
false
,
"message"
:
"state is empty or not same"
,
})
return
}
username
:=
session
.
Get
(
"username"
)
username
:=
session
.
Get
(
"username"
)
if
username
!=
nil
{
if
username
!=
nil
{
GitHubBind
(
c
)
GitHubBind
(
c
)
...
@@ -205,3 +213,22 @@ func GitHubBind(c *gin.Context) {
...
@@ -205,3 +213,22 @@ func GitHubBind(c *gin.Context) {
})
})
return
return
}
}
func
GenerateOAuthCode
(
c
*
gin
.
Context
)
{
session
:=
sessions
.
Default
(
c
)
state
:=
common
.
GetRandomString
(
12
)
session
.
Set
(
"oauth_state"
,
state
)
err
:=
session
.
Save
()
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"message"
:
err
.
Error
(),
})
return
}
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
true
,
"message"
:
""
,
"data"
:
state
,
})
}
router/api-router.go
View file @
8c779729
...
@@ -21,6 +21,7 @@ func SetApiRouter(router *gin.Engine) {
...
@@ -21,6 +21,7 @@ func SetApiRouter(router *gin.Engine) {
apiRouter
.
GET
(
"/reset_password"
,
middleware
.
CriticalRateLimit
(),
middleware
.
TurnstileCheck
(),
controller
.
SendPasswordResetEmail
)
apiRouter
.
GET
(
"/reset_password"
,
middleware
.
CriticalRateLimit
(),
middleware
.
TurnstileCheck
(),
controller
.
SendPasswordResetEmail
)
apiRouter
.
POST
(
"/user/reset"
,
middleware
.
CriticalRateLimit
(),
controller
.
ResetPassword
)
apiRouter
.
POST
(
"/user/reset"
,
middleware
.
CriticalRateLimit
(),
controller
.
ResetPassword
)
apiRouter
.
GET
(
"/oauth/github"
,
middleware
.
CriticalRateLimit
(),
controller
.
GitHubOAuth
)
apiRouter
.
GET
(
"/oauth/github"
,
middleware
.
CriticalRateLimit
(),
controller
.
GitHubOAuth
)
apiRouter
.
GET
(
"/oauth/state"
,
middleware
.
CriticalRateLimit
(),
controller
.
GenerateOAuthCode
)
apiRouter
.
GET
(
"/oauth/wechat"
,
middleware
.
CriticalRateLimit
(),
controller
.
WeChatAuth
)
apiRouter
.
GET
(
"/oauth/wechat"
,
middleware
.
CriticalRateLimit
(),
controller
.
WeChatAuth
)
apiRouter
.
GET
(
"/oauth/wechat/bind"
,
middleware
.
CriticalRateLimit
(),
middleware
.
UserAuth
(),
controller
.
WeChatBind
)
apiRouter
.
GET
(
"/oauth/wechat/bind"
,
middleware
.
CriticalRateLimit
(),
middleware
.
UserAuth
(),
controller
.
WeChatBind
)
apiRouter
.
GET
(
"/oauth/email/bind"
,
middleware
.
CriticalRateLimit
(),
middleware
.
UserAuth
(),
controller
.
EmailBind
)
apiRouter
.
GET
(
"/oauth/email/bind"
,
middleware
.
CriticalRateLimit
(),
middleware
.
UserAuth
(),
controller
.
EmailBind
)
...
...
web/src/components/GitHubOAuth.js
View file @
8c779729
...
@@ -13,8 +13,8 @@ const GitHubOAuth = () => {
...
@@ -13,8 +13,8 @@ const GitHubOAuth = () => {
let
navigate
=
useNavigate
();
let
navigate
=
useNavigate
();
const
sendCode
=
async
(
code
,
count
)
=>
{
const
sendCode
=
async
(
code
,
state
,
count
)
=>
{
const
res
=
await
API
.
get
(
`/api/oauth/github?code=
${
code
}
`
);
const
res
=
await
API
.
get
(
`/api/oauth/github?code=
${
code
}
&state=
${
state
}
`
);
const
{
success
,
message
,
data
}
=
res
.
data
;
const
{
success
,
message
,
data
}
=
res
.
data
;
if
(
success
)
{
if
(
success
)
{
if
(
message
===
'bind'
)
{
if
(
message
===
'bind'
)
{
...
@@ -36,13 +36,14 @@ const GitHubOAuth = () => {
...
@@ -36,13 +36,14 @@ const GitHubOAuth = () => {
count
++
;
count
++
;
setPrompt
(
`出现错误,第
${
count
}
次重试中...`
);
setPrompt
(
`出现错误,第
${
count
}
次重试中...`
);
await
new
Promise
((
resolve
)
=>
setTimeout
(
resolve
,
count
*
2000
));
await
new
Promise
((
resolve
)
=>
setTimeout
(
resolve
,
count
*
2000
));
await
sendCode
(
code
,
count
);
await
sendCode
(
code
,
state
,
count
);
}
}
};
};
useEffect
(()
=>
{
useEffect
(()
=>
{
let
code
=
searchParams
.
get
(
'code'
);
let
code
=
searchParams
.
get
(
'code'
);
sendCode
(
code
,
0
).
then
();
let
state
=
searchParams
.
get
(
'state'
);
sendCode
(
code
,
state
,
0
).
then
();
},
[]);
},
[]);
return
(
return
(
...
...
web/src/components/LoginForm.js
View file @
8c779729
...
@@ -3,6 +3,7 @@ import { Button, Divider, Form, Grid, Header, Image, Message, Modal, Segment } f
...
@@ -3,6 +3,7 @@ import { Button, Divider, Form, Grid, Header, Image, Message, Modal, Segment } f
import
{
Link
,
useNavigate
,
useSearchParams
}
from
'react-router-dom'
;
import
{
Link
,
useNavigate
,
useSearchParams
}
from
'react-router-dom'
;
import
{
UserContext
}
from
'../context/User'
;
import
{
UserContext
}
from
'../context/User'
;
import
{
API
,
getLogo
,
showError
,
showSuccess
}
from
'../helpers'
;
import
{
API
,
getLogo
,
showError
,
showSuccess
}
from
'../helpers'
;
import
{
getOAuthState
,
onGitHubOAuthClicked
}
from
'./utils'
;
const
LoginForm
=
()
=>
{
const
LoginForm
=
()
=>
{
const
[
inputs
,
setInputs
]
=
useState
({
const
[
inputs
,
setInputs
]
=
useState
({
...
@@ -31,12 +32,6 @@ const LoginForm = () => {
...
@@ -31,12 +32,6 @@ const LoginForm = () => {
const
[
showWeChatLoginModal
,
setShowWeChatLoginModal
]
=
useState
(
false
);
const
[
showWeChatLoginModal
,
setShowWeChatLoginModal
]
=
useState
(
false
);
const
onGitHubOAuthClicked
=
()
=>
{
window
.
open
(
`https://github.com/login/oauth/authorize?client_id=
${
status
.
github_client_id
}
&scope=user:email`
);
};
const
onWeChatLoginClicked
=
()
=>
{
const
onWeChatLoginClicked
=
()
=>
{
setShowWeChatLoginModal
(
true
);
setShowWeChatLoginModal
(
true
);
};
};
...
@@ -131,7 +126,7 @@ const LoginForm = () => {
...
@@ -131,7 +126,7 @@ const LoginForm = () => {
circular
circular
color
=
'black'
color
=
'black'
icon
=
'github'
icon
=
'github'
onClick
=
{
onGitHubOAuthClicked
}
onClick
=
{
()
=>
onGitHubOAuthClicked
(
status
.
github_client_id
)
}
/
>
/
>
)
:
(
)
:
(
<><
/
>
<><
/
>
...
...
web/src/components/PersonalSetting.js
View file @
8c779729
...
@@ -4,6 +4,7 @@ import { Link, useNavigate } from 'react-router-dom';
...
@@ -4,6 +4,7 @@ import { Link, useNavigate } from 'react-router-dom';
import
{
API
,
copy
,
showError
,
showInfo
,
showNotice
,
showSuccess
}
from
'../helpers'
;
import
{
API
,
copy
,
showError
,
showInfo
,
showNotice
,
showSuccess
}
from
'../helpers'
;
import
Turnstile
from
'react-turnstile'
;
import
Turnstile
from
'react-turnstile'
;
import
{
UserContext
}
from
'../context/User'
;
import
{
UserContext
}
from
'../context/User'
;
import
{
onGitHubOAuthClicked
}
from
'./utils'
;
const
PersonalSetting
=
()
=>
{
const
PersonalSetting
=
()
=>
{
const
[
userState
,
userDispatch
]
=
useContext
(
UserContext
);
const
[
userState
,
userDispatch
]
=
useContext
(
UserContext
);
...
@@ -130,12 +131,6 @@ const PersonalSetting = () => {
...
@@ -130,12 +131,6 @@ const PersonalSetting = () => {
}
}
};
};
const
openGitHubOAuth
=
()
=>
{
window
.
open
(
`https://github.com/login/oauth/authorize?client_id=
${
status
.
github_client_id
}
&scope=user:email`
);
};
const
sendVerificationCode
=
async
()
=>
{
const
sendVerificationCode
=
async
()
=>
{
setDisableButton
(
true
);
setDisableButton
(
true
);
if
(
inputs
.
email
===
''
)
return
;
if
(
inputs
.
email
===
''
)
return
;
...
@@ -249,7 +244,7 @@ const PersonalSetting = () => {
...
@@ -249,7 +244,7 @@ const PersonalSetting = () => {
<
/Modal
>
<
/Modal
>
{
{
status
.
github_oauth
&&
(
status
.
github_oauth
&&
(
<
Button
onClick
=
{
openGitHubOAuth
}
>
绑定
GitHub
账号
<
/Button
>
<
Button
onClick
=
{
()
=>
{
onGitHubOAuthClicked
(
status
.
github_client_id
)}
}
>
绑定
GitHub
账号
<
/Button
>
)
)
}
}
<
Button
<
Button
...
...
web/src/components/utils.js
0 → 100644
View file @
8c779729
import
{
API
,
showError
}
from
'../helpers'
;
export
async
function
getOAuthState
()
{
const
res
=
await
API
.
get
(
'/api/oauth/state'
);
const
{
success
,
message
,
data
}
=
res
.
data
;
if
(
success
)
{
return
data
;
}
else
{
showError
(
message
);
return
''
;
}
}
export
async
function
onGitHubOAuthClicked
(
github_client_id
)
{
const
state
=
await
getOAuthState
();
if
(
!
state
)
return
;
window
.
open
(
`https://github.com/login/oauth/authorize?client_id=
${
github_client_id
}
&state=
${
state
}
&scope=user:email`
);
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment