1 Star 4 Fork 0

jingshanccc/course

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
oauth.go 1.51 KB
一键复制 编辑 原始数据 按行查看 历史
jingshanccc 提交于 2021-04-02 16:15 . [feature] #76
package handler
import (
"context"
"gitee.com/jingshanccc/course/gateway/middleware"
"gitee.com/jingshanccc/course/public"
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
//Authorize: oauth获取授权码
func Authorize(ctx *gin.Context) {
err := middleware.AuthServer.HandleAuthorizeRequest(ctx.Writer, ctx.Request)
if err != nil {
ctx.AbortWithError(http.StatusUnauthorized, err)
}
}
//Token: oauth获取令牌
func Token(ctx *gin.Context) {
err := middleware.AuthServer.HandleTokenRequest(ctx.Writer, ctx.Request)
if err != nil {
ctx.AbortWithError(http.StatusUnauthorized, err)
}
}
//Redirect: oauth跳转地址 完成将授权码和client信息重定向到获取令牌接口的工作
func Redirect(ctx *gin.Context) {
client, _ := middleware.AuthServer.Manager.GetClient(context.Background(), ctx.Request.FormValue("state"))
postValue := url.Values{
"code": {ctx.Request.FormValue("code")},
"client_id": {client.GetID()},
"client_secret": {client.GetSecret()},
"grant_type": {"authorization_code"},
"redirect_uri": {"http://" + ctx.Request.Host + strings.Split(ctx.Request.RequestURI, "?")[0]},
}
resp, err := http.PostForm("http://"+ctx.Request.Host+"/api/v1/oauth/token", postValue)
if err != nil {
ctx.AbortWithError(http.StatusUnauthorized, err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
ctx.AbortWithError(http.StatusUnauthorized, err)
}
content := string(body)
public.ResponseSuccess(ctx, content)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jingshanccc/course.git
git@gitee.com:jingshanccc/course.git
jingshanccc
course
course
23f538baa694

搜索帮助