mirror of https://github.com/synctv-org/synctv
Feat: user bind unbind providers
parent
82ca47be58
commit
7d4a60a5b3
@ -0,0 +1,57 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/synctv-org/synctv/internal/db"
|
||||
"github.com/synctv-org/synctv/internal/op"
|
||||
"github.com/synctv-org/synctv/internal/provider"
|
||||
"github.com/synctv-org/synctv/internal/provider/providers"
|
||||
"github.com/synctv-org/synctv/server/model"
|
||||
"github.com/synctv-org/synctv/utils"
|
||||
)
|
||||
|
||||
func BindApi(ctx *gin.Context) {
|
||||
user := ctx.MustGet("user").(*op.User)
|
||||
|
||||
pi, err := providers.GetProvider(provider.OAuth2Provider(ctx.Param("type")))
|
||||
if err != nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
|
||||
}
|
||||
|
||||
meta := model.OAuth2Req{}
|
||||
if err := model.Decode(ctx, &meta); err != nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
|
||||
return
|
||||
}
|
||||
|
||||
state := utils.RandString(16)
|
||||
states.Store(state, stateMeta{
|
||||
OAuth2Req: meta,
|
||||
BindUserId: user.ID,
|
||||
}, time.Minute*5)
|
||||
|
||||
ctx.JSON(http.StatusOK, model.NewApiDataResp(gin.H{
|
||||
"url": pi.NewAuthURL(state),
|
||||
}))
|
||||
}
|
||||
|
||||
func UnBindApi(ctx *gin.Context) {
|
||||
user := ctx.MustGet("user").(*op.User)
|
||||
|
||||
pi, err := providers.GetProvider(provider.OAuth2Provider(ctx.Param("type")))
|
||||
if err != nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
|
||||
return
|
||||
}
|
||||
|
||||
err = db.UnBindProvider(user.ID, pi.Provider())
|
||||
if err != nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, model.NewApiErrorResp(err))
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Status(http.StatusNoContent)
|
||||
}
|
@ -1,19 +1,28 @@
|
||||
package auth
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/synctv-org/synctv/server/middlewares"
|
||||
)
|
||||
|
||||
func Init(e *gin.Engine) {
|
||||
{
|
||||
auth := e.Group("/oauth2")
|
||||
oauth2 := e.Group("/oauth2")
|
||||
needAuthOauth2 := oauth2.Group("")
|
||||
needAuthOauth2.Use(middlewares.AuthUserMiddleware)
|
||||
|
||||
auth.GET("/enabled", OAuth2EnabledApi)
|
||||
oauth2.GET("/enabled", OAuth2EnabledApi)
|
||||
|
||||
auth.GET("/login/:type", OAuth2)
|
||||
oauth2.GET("/login/:type", OAuth2)
|
||||
|
||||
auth.POST("/login/:type", OAuth2Api)
|
||||
oauth2.POST("/login/:type", OAuth2Api)
|
||||
|
||||
auth.GET("/callback/:type", OAuth2Callback)
|
||||
oauth2.GET("/callback/:type", OAuth2Callback)
|
||||
|
||||
auth.POST("/callback/:type", OAuth2CallbackApi)
|
||||
oauth2.POST("/callback/:type", OAuth2CallbackApi)
|
||||
|
||||
needAuthOauth2.POST("/bind/:type", BindApi)
|
||||
|
||||
needAuthOauth2.POST("/unbind/:type", UnBindApi)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue