-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from idoknow/feat/oauth-server
Feat: OAuth 2.0 Server
- Loading branch information
Showing
25 changed files
with
943 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package controller | ||
|
||
import ( | ||
"github.com/RockChinQ/Campux/backend/database" | ||
"github.com/RockChinQ/Campux/backend/service" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type AdminRouter struct { | ||
APIRouter | ||
AdminService service.AdminService | ||
} | ||
|
||
func NewAdminRouter(rg *gin.RouterGroup, as service.AdminService) *AdminRouter { | ||
ar := &AdminRouter{ | ||
AdminService: as, | ||
} | ||
|
||
group := rg.Group("/admin") | ||
|
||
// bind routes | ||
group.POST("/add-oauth2-app", ar.AddOAuth2App) | ||
group.GET("/get-oauth2-apps", ar.GetOAuth2AppList) | ||
group.DELETE("/del-oauth2-app/:id", ar.DeleteOAuth2App) | ||
|
||
return ar | ||
} | ||
|
||
// 添加一个OAuth2应用 | ||
func (ar *AdminRouter) AddOAuth2App(c *gin.Context) { | ||
|
||
uin, err := ar.Auth(c, UserOnly) | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
if !ar.AdminService.CheckUserGroup(uin, []database.UserGroup{ | ||
database.USER_GROUP_ADMIN, | ||
}) { | ||
ar.StatusCode(c, 401, "权限不足") | ||
return | ||
} | ||
|
||
// 取body的json里的appname | ||
var body OAuth2AppCreateBody | ||
|
||
if err := c.ShouldBindJSON(&body); err != nil { | ||
ar.Fail(c, 1, err.Error()) | ||
return | ||
} | ||
|
||
// 创建OAuth2应用 | ||
app, err := ar.AdminService.AddOAuth2App(body.Name, body.Emoji) | ||
|
||
if err != nil { | ||
ar.Fail(c, 2, err.Error()) | ||
return | ||
} | ||
|
||
ar.Success(c, app) | ||
} | ||
|
||
// 获取 OAuth2 应用列表 | ||
func (ar *AdminRouter) GetOAuth2AppList(c *gin.Context) { | ||
uin, err := ar.Auth(c, UserOnly) | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
if !ar.AdminService.CheckUserGroup(uin, []database.UserGroup{ | ||
database.USER_GROUP_ADMIN, | ||
}) { | ||
ar.StatusCode(c, 401, "权限不足") | ||
return | ||
} | ||
|
||
// 获取OAuth2应用列表 | ||
list, err := ar.AdminService.GetOAuth2Apps() | ||
|
||
if err != nil { | ||
ar.Fail(c, 1, err.Error()) | ||
return | ||
} | ||
|
||
ar.Success(c, gin.H{ | ||
"list": list, | ||
}) | ||
} | ||
|
||
// 删除一个OAuth2应用 | ||
func (ar *AdminRouter) DeleteOAuth2App(c *gin.Context) { | ||
uin, err := ar.Auth(c, UserOnly) | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
if !ar.AdminService.CheckUserGroup(uin, []database.UserGroup{ | ||
database.USER_GROUP_ADMIN, | ||
}) { | ||
ar.StatusCode(c, 401, "权限不足") | ||
return | ||
} | ||
|
||
// 取路由参数 | ||
appID := c.Param("id") | ||
|
||
// 删除OAuth2应用 | ||
err = ar.AdminService.DeleteOAuth2App(appID) | ||
|
||
if err != nil { | ||
ar.Fail(c, 1, err.Error()) | ||
return | ||
} | ||
|
||
ar.Success(c, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package controller | ||
|
||
import ( | ||
"github.com/RockChinQ/Campux/backend/service" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type OAuth2Router struct { | ||
APIRouter | ||
OAuth2Service service.OAuth2Service | ||
} | ||
|
||
func NewOAuth2Router(rg *gin.RouterGroup, oas service.OAuth2Service) *OAuth2Router { | ||
|
||
oar := &OAuth2Router{ | ||
OAuth2Service: oas, | ||
} | ||
|
||
group := rg.Group("/oauth2") | ||
|
||
group.GET("/get-app-info", oar.GetOAuth2AppInfo) | ||
group.GET("/authorize", oar.Authorize) | ||
group.POST("/get-access-token", oar.GetAccessToken) | ||
|
||
return oar | ||
} | ||
|
||
func (oar *OAuth2Router) GetOAuth2AppInfo(c *gin.Context) { | ||
clientID := c.Query("client_id") | ||
|
||
app, err := oar.OAuth2Service.GetOAuth2AppByClientID(clientID) | ||
|
||
if err != nil { | ||
oar.Fail(c, 1, err.Error()) | ||
return | ||
} | ||
|
||
if app == nil { | ||
oar.Fail(c, 2, "此应用未注册") | ||
return | ||
} | ||
|
||
oar.Success(c, gin.H{ | ||
"client_id": app.ClientID, | ||
"name": app.Name, | ||
}) | ||
} | ||
|
||
func (oar *OAuth2Router) Authorize(c *gin.Context) { | ||
|
||
uin, err := oar.Auth(c, UserOnly) | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
clientID := c.Query("client_id") | ||
|
||
if clientID == "" { | ||
oar.Fail(c, 1, "未提供 client_id") | ||
return | ||
} | ||
|
||
// 检查是否存在这个应用 | ||
app, err := oar.OAuth2Service.GetOAuth2AppByClientID(clientID) | ||
|
||
if err != nil { | ||
oar.Fail(c, 2, err.Error()) | ||
return | ||
} | ||
|
||
if app == nil { | ||
oar.Fail(c, 3, "此应用未注册") | ||
return | ||
} | ||
|
||
// 计算code | ||
code, err := oar.OAuth2Service.GenerateCode(app.ClientID, uin) | ||
|
||
if err != nil { | ||
oar.Fail(c, 4, err.Error()) | ||
return | ||
} | ||
|
||
oar.Success(c, gin.H{ | ||
"code": code, | ||
}) | ||
} | ||
|
||
func (oar *OAuth2Router) GetAccessToken(c *gin.Context) { | ||
|
||
var body OAuth2GetAccessTokenBody | ||
|
||
if err := c.ShouldBindJSON(&body); err != nil { | ||
oar.Fail(c, 1, err.Error()) | ||
return | ||
} | ||
|
||
// 检查code | ||
ak, err := oar.OAuth2Service.GetAccessToken(body.ClientID, body.ClientSecret, body.Code) | ||
|
||
if err != nil { | ||
oar.Fail(c, 2, err.Error()) | ||
return | ||
} | ||
|
||
oar.Success(c, gin.H{ | ||
"access_token": ak, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.