dongwanqiang_2017 2017-11-02 21:38
浏览 110

Gin Sessions将状态和代码存储在URL中,我想更改它以使URL更加整洁

I am making a backend using go, the backend should get the google calendar of the user who login in the site using google account. I am using gin to do the routing and sessions from this package github.com/gin-gonic/contrib/sessions

this is my main method

func main() {
    router := gin.Default()
    var store = sessions.NewCookieStore([]byte("secret"))
    router.Use(sessions.Sessions("goquestsession", store))

    router.GET("/", indexHandler)
    router.GET("/login", loginHandler)
    router.GET("/auth", authHandler)

    router.Run("127.0.0.1:9090")
}

login handler makes a new session for the user with random ID and send it to the user

func loginHandler(c *gin.Context) {
    state = randToken()
    session := sessions.Default(c)
    session.Set("state", state)
    session.Save()

    c.Writer.Write([]byte("<html><title>Golang Google</title> <body> <a href='" + getLoginURL(state) + "'><button>Login with Google!</button> </a> </body></html>"))
}

then I am using the auth handler to test the code and the state and get the calendar

func authHandler(c *gin.Context) {
    // Handle the exchange code to initiate a transport.
    session := sessions.Default(c)

    retrievedState := session.Get("state")
    queryState := c.Request.URL.Query().Get("state")
    if retrievedState != queryState {
        c.AbortWithError(http.StatusUnauthorized, fmt.Errorf("Invalid session state: %s", retrievedState))
        return
    }
    code := c.Request.URL.Query().Get("code")
    tok, err := conf.Exchange(oauth2.NoContext, code)
    if err != nil {
        c.AbortWithError(http.StatusBadRequest, err)
        return
    }

    client := conf.Client(oauth2.NoContext, tok)
    calendarService, _ := calendar.New(client)
    list, err := calendarService.CalendarList.List().Do()
    if err != nil {
        fmt.Println("error")
    }
    log.Println(list.Items[0].Summary)
    c.Status(http.StatusOK)
}

after writing the code I find out that the link of is like this

http://127.0.0.1:9090/auth?state=BuQ8DyhTEgivb6CatcTzJg8sk2Nb6EUStRkdgGVvDRE%3D&code=4/Qit73p0btO0RRM93_YmjlP0Ex2dqDLsP3JVdrnhNE7Y#

now after looking in the package github.com/gin-gonic/contrib/sessions I didn't find a way to just be like this http://127.0.0.1:9090/auth and save the rest of the link somewhere else do I have to keep using URL like this if I am using Gin or there is some other way to make my link look cleaner

  • 写回答

1条回答 默认 最新

  • dtnwm4807 2017-11-02 21:52
    关注

    I would personally add a route for the calendar, and then save the code to the session and redirect, so the auth handler url is never really visible, it just sends back a redirect to a clean url:

    router.GET("/calendar", calendarHandler)
    

    And then do:

    func authHandler(c *gin.Context) {
        // Handle the exchange code to initiate a transport.
        session := sessions.Default(c)
    
        retrievedState := session.Get("state")
        queryState := c.Request.URL.Query().Get("state")
        if retrievedState != queryState {
            c.AbortWithError(http.StatusUnauthorized, fmt.Errorf("Invalid session state: %s", retrievedState))
            return
        }
        code := c.Request.URL.Query().Get("code")
        session.Set("code", code)
        session.Save()
        c.Redirect(http.StatusFound, "/calendar")
    }
    
    func calendarHandler(c *gin.Context) {
        session := sessions.Default(c)
        code := session.Get("code")
        tok, err := conf.Exchange(oauth2.NoContext, code)
        if err != nil {
            c.AbortWithError(http.StatusBadRequest, err)
            return
        }
    
        client := conf.Client(oauth2.NoContext, tok)
        calendarService, _ := calendar.New(client)
        list, err := calendarService.CalendarList.List().Do()
        if err != nil {
            fmt.Println("error")
        }
        log.Println(list.Items[0].Summary)
        c.Status(http.StatusOK)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图