update
1. add youtube command 2. add google webhook 3. add twitch oauth route 4. add api checkSession middleware
This commit is contained in:
+38
-4
@@ -2,12 +2,47 @@ package api
|
||||
|
||||
import (
|
||||
"git.trj.tw/golang/mtfosbot/model"
|
||||
"git.trj.tw/golang/mtfosbot/module/apis/twitch"
|
||||
"git.trj.tw/golang/mtfosbot/module/context"
|
||||
"git.trj.tw/golang/mtfosbot/module/utils"
|
||||
"github.com/gin-gonic/contrib/sessions"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// CheckSession -
|
||||
func CheckSession(c *context.Context) {
|
||||
session := sessions.Default(c.Context)
|
||||
userData := session.Get("user")
|
||||
loginType := session.Get("loginType")
|
||||
if userData == nil || loginType == nil {
|
||||
c.LoginFirst(nil)
|
||||
return
|
||||
}
|
||||
var name string
|
||||
var ltype string
|
||||
var ok bool
|
||||
switch userData.(type) {
|
||||
case model.Account:
|
||||
name = userData.(model.Account).Account
|
||||
case twitch.UserInfo:
|
||||
name = userData.(twitch.UserInfo).DisplayName
|
||||
default:
|
||||
c.LoginFirst(nil)
|
||||
return
|
||||
}
|
||||
if ltype, ok = loginType.(string); !ok {
|
||||
c.LoginFirst(nil)
|
||||
return
|
||||
}
|
||||
|
||||
loginUser := map[string]string{
|
||||
"name": name,
|
||||
"type": ltype,
|
||||
}
|
||||
session.Set("loginUser", loginUser)
|
||||
session.Save()
|
||||
c.Next()
|
||||
}
|
||||
|
||||
// UserLogin - system user login
|
||||
func UserLogin(c *context.Context) {
|
||||
bodyArg := struct {
|
||||
@@ -32,11 +67,10 @@ func UserLogin(c *context.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
accInt := utils.ToMap(acc)
|
||||
delete(accInt, "password")
|
||||
session := sessions.Default(c.Context)
|
||||
|
||||
session.Set("user", accInt)
|
||||
acc.Password = ""
|
||||
session.Set("user", acc)
|
||||
session.Set("loginType", "system")
|
||||
session.Save()
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"git.trj.tw/golang/mtfosbot/router/api"
|
||||
"git.trj.tw/golang/mtfosbot/router/google"
|
||||
"git.trj.tw/golang/mtfosbot/router/line"
|
||||
"git.trj.tw/golang/mtfosbot/router/twitch"
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -44,6 +45,7 @@ func SetRoutes(r *gin.Engine) {
|
||||
apiGroup := r.Group("/api")
|
||||
{
|
||||
apiGroup.POST("/login", context.PatchCtx(api.UserLogin))
|
||||
apiGroup.POST("/logout", context.PatchCtx(api.UserLogout))
|
||||
}
|
||||
|
||||
lineApis := r.Group("/line")
|
||||
@@ -56,4 +58,10 @@ func SetRoutes(r *gin.Engine) {
|
||||
googleApis.GET("/youtube/webhook", context.PatchCtx(google.VerifyWebhook))
|
||||
googleApis.POST("/youtube/webhook", context.PatchCtx(google.GetNotifyWebhook))
|
||||
}
|
||||
|
||||
twitchApis := r.Group("/twitch")
|
||||
{
|
||||
twitchApis.GET("/login", context.PatchCtx(twitch.OAuthLogin))
|
||||
twitchApis.GET("/oauth", context.PatchCtx(twitch.OAuthProc))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
package twitch
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"git.trj.tw/golang/mtfosbot/model"
|
||||
twitchapi "git.trj.tw/golang/mtfosbot/module/apis/twitch"
|
||||
"git.trj.tw/golang/mtfosbot/module/config"
|
||||
"git.trj.tw/golang/mtfosbot/module/context"
|
||||
"github.com/gin-gonic/contrib/sessions"
|
||||
)
|
||||
|
||||
// OAuthLogin -
|
||||
func OAuthLogin(c *context.Context) {
|
||||
session := sessions.Default(c.Context)
|
||||
conf := config.GetConf()
|
||||
redirectTo := strings.TrimRight(conf.URL, "/")
|
||||
redirectTo += "/twitch/oauth"
|
||||
qs := url.Values{}
|
||||
qs.Add("client_id", conf.Twitch.ClientID)
|
||||
qs.Add("redirect_uri", redirectTo)
|
||||
qs.Add("response_type", "code")
|
||||
qs.Add("scope", "user:read:email")
|
||||
|
||||
toURL, ok := c.GetQuery("tourl")
|
||||
if ok && len(toURL) > 0 {
|
||||
session.Set("backUrl", toURL)
|
||||
session.Save()
|
||||
}
|
||||
|
||||
u, err := url.Parse(redirectTo)
|
||||
if err != nil {
|
||||
c.ServerError(nil)
|
||||
return
|
||||
}
|
||||
finalURL, err := u.Parse(qs.Encode())
|
||||
if err != nil {
|
||||
c.ServerError(nil)
|
||||
return
|
||||
}
|
||||
c.Redirect(301, finalURL.String())
|
||||
}
|
||||
|
||||
// OAuthProc -
|
||||
func OAuthProc(c *context.Context) {
|
||||
code, ok := c.GetQuery("code")
|
||||
if !ok || len(code) == 0 {
|
||||
c.DataFormat(nil)
|
||||
return
|
||||
}
|
||||
|
||||
tokenData, err := twitchapi.GetTokenData(code)
|
||||
if err != nil {
|
||||
c.DataFormat(nil)
|
||||
return
|
||||
}
|
||||
|
||||
session := sessions.Default(c.Context)
|
||||
|
||||
userData := twitchapi.GetUserDataByToken(tokenData.AccessToken)
|
||||
if userData == nil {
|
||||
c.ServerError(nil)
|
||||
return
|
||||
}
|
||||
|
||||
session.Set("token", tokenData)
|
||||
session.Set("user", userData)
|
||||
session.Set("loginType", "twitch")
|
||||
|
||||
chData, err := model.GetTwitchChannelWithID(userData.ID)
|
||||
if err != nil {
|
||||
c.ServerError(nil)
|
||||
return
|
||||
}
|
||||
if chData == nil {
|
||||
chData = &model.TwitchChannel{
|
||||
ID: userData.ID,
|
||||
Name: userData.DisplayName,
|
||||
}
|
||||
err = chData.Add()
|
||||
if err != nil {
|
||||
c.ServerError(nil)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if userData.DisplayName != chData.Name {
|
||||
chData.UpdateName(userData.DisplayName)
|
||||
}
|
||||
}
|
||||
|
||||
conf := config.GetConf()
|
||||
goURL := strings.TrimRight(conf.URL, "/") + "/web"
|
||||
tourl := session.Get("backUrl")
|
||||
if tourl != nil {
|
||||
if str, ok := tourl.(string); ok {
|
||||
goURL = str
|
||||
session.Delete("backUrl")
|
||||
}
|
||||
}
|
||||
session.Save()
|
||||
c.Redirect(301, goURL)
|
||||
}
|
||||
Reference in New Issue
Block a user