continued auth system
This commit is contained in:
@@ -2,6 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -49,8 +50,26 @@ func (server *ApiServer) loadEndpoints() {
|
||||
})
|
||||
|
||||
server.engine.GET("/auth", func(c *gin.Context) {
|
||||
q := c.Request.URL.Query()
|
||||
if resp := loadAuthQueryOk(q); resp != nil {
|
||||
// ok
|
||||
// TODO check state (need state system)
|
||||
// TODO POST https://id.twitch.tv/oauth2/token - returns TwitchAuthTokenResp
|
||||
// convert expiresIn to time.Time (minus like 15 minutes as a buffer period)
|
||||
// UpdateUserAuth()
|
||||
// TODO return twitch ok (or err if can't POST)
|
||||
} else if resp := loadAuthQueryErr(q); resp != nil {
|
||||
// err from twitch
|
||||
// TODO check state (need state system)
|
||||
// TODO return twitch err
|
||||
} else {
|
||||
// err in params
|
||||
// TODO return param err
|
||||
}
|
||||
|
||||
// TODO auth response from twitch
|
||||
// parse args as TwitchAuthRespOk or TwitchAuthRespErr
|
||||
// verify state with db and client id with config
|
||||
c.JSON(http.StatusOK, serverInfo)
|
||||
})
|
||||
}
|
||||
@@ -69,6 +88,28 @@ type TwitchAuthParams struct {
|
||||
State string `json:"state"`
|
||||
}
|
||||
|
||||
func loadAuthQueryOk(query url.Values) *TwitchAuthRespOk {
|
||||
if query.Has("code") && query.Has("scope") && query.Has("state") {
|
||||
return &TwitchAuthRespOk{
|
||||
Code: query.Get("code"),
|
||||
Scope: query.Get("scope"),
|
||||
State: query.Get("state"),
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadAuthQueryErr(query url.Values) *TwitchAuthRespErr {
|
||||
if query.Has("error") && query.Has("error_description") && query.Has("state") {
|
||||
return &TwitchAuthRespErr{
|
||||
Err: query.Get("error"),
|
||||
ErrDesc: query.Get("error_description"),
|
||||
State: query.Get("state"),
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type TwitchAuthRespOk struct {
|
||||
Code string `json:"code"`
|
||||
Scope string `json:"scope"`
|
||||
@@ -80,3 +121,11 @@ type TwitchAuthRespErr struct {
|
||||
ErrDesc string `json:"error_description"`
|
||||
State string `json:"state"`
|
||||
}
|
||||
|
||||
type TwitchAuthTokenResp struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
Scope []string `json:"scope"`
|
||||
TokenType string `json:"token_type"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user