dsio68964998 2017-04-13 02:24
浏览 64

API验证流程

I'm currently writing an API in Go and have been racking my brain over how to do authentication/authorization correctly and securely.

As I understand it, this is how it goes:

  • New user registers for account via api/user/register endpoint (or api/user/login for existing users)
  • Server receives request and checks that username is unique, etc. After that, it issues (if all is well) an access token and refresh token, both signed for added security.
  • The client app receives the tokens and stores them in the browser cookie (or local/session Storage) and makes sure to send them securely over HTTPS in any subsequent requests to the API.
  • When receiving requests to protected routes, the server checks the access token's expiry date, and if expired, will check the refresh token's validity in the database. If it's invalid, ask for reauthentication clientside. Otherwise, reissue a new access token.

My questions are regarding the steps dealing with refresh tokens.

I am also writing the client application (in React); I won't be releasing the API to the public. I simply am writing the backend as an API for the client app.

  • Should I still use refresh tokens?
  • Do I need an api/auth/token route? I keep reading about them in implementation examples and I feel like I can just have some helper functions to query the database and reissue tokens in my backend code instead of having to query another endpoint to do so.

Sorry if they're dumb questions, but I've been poring over page after page detailing the auth spec, and the subtle differences from page to page are leaving me confused and unsure of what is truly "best practice" in production.

  • 写回答

1条回答 默认 最新

  • douquan1015 2017-05-14 14:50
    关注

    I think you are confusing this over the word login. Instead of /api/user/login I call it /api/user/authentication. So if the request has a json attached to its body, it return a valid token. But if the request got a Authentication Header that is valid, you just issue a new token valid for the same period of time. This is specially good for frontends, so you could try to re-auth automatically.

    newUser := types.User{}
    if r.Body != nil {
         err := json.NewDecoder(r.Body).Decode(&newUser)
         ... 
    }
    authHeader := r.Header.Get("Authorization")
    if authHeader != "" {
        _, err := USERAUTH.CHeckJWT(w,r)
        if err !=nil {
        ...,
        }
        newToken := GenerateTokenFromToken(token)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分