forked from techschool/simplebank
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add session and refresh_token to the api (techschool#25)
Co-authored-by: phamlequang <phamlequang@gmail.com>
- Loading branch information
1 parent
0a2b7b7
commit 5663a67
Showing
21 changed files
with
915 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package api | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type renewAccessTokenRequest struct { | ||
RefreshToken string `json:"refresh_token" binding:"required"` | ||
} | ||
|
||
type renewAccessTokenResponse struct { | ||
AccessToken string `json:"access_token"` | ||
AccessTokenExpiresAt time.Time `json:"access_token_expires_at"` | ||
} | ||
|
||
func (server *Server) renewAccessToken(ctx *gin.Context) { | ||
var req renewAccessTokenRequest | ||
if err := ctx.ShouldBindJSON(&req); err != nil { | ||
ctx.JSON(http.StatusBadRequest, errorResponse(err)) | ||
return | ||
} | ||
|
||
refreshPayload, err := server.tokenMaker.VerifyToken(req.RefreshToken) | ||
if err != nil { | ||
ctx.JSON(http.StatusUnauthorized, errorResponse(err)) | ||
return | ||
} | ||
|
||
session, err := server.store.GetSession(ctx, refreshPayload.ID) | ||
if err != nil { | ||
if err == sql.ErrNoRows { | ||
ctx.JSON(http.StatusNotFound, errorResponse(err)) | ||
return | ||
} | ||
ctx.JSON(http.StatusInternalServerError, errorResponse(err)) | ||
return | ||
} | ||
|
||
if session.IsBlocked { | ||
err := fmt.Errorf("blocked session") | ||
ctx.JSON(http.StatusUnauthorized, errorResponse(err)) | ||
return | ||
} | ||
|
||
if session.Username != refreshPayload.Username { | ||
err := fmt.Errorf("incorrect session user") | ||
ctx.JSON(http.StatusUnauthorized, errorResponse(err)) | ||
return | ||
} | ||
|
||
if session.RefreshToken != req.RefreshToken { | ||
err := fmt.Errorf("mismatched session token") | ||
ctx.JSON(http.StatusUnauthorized, errorResponse(err)) | ||
return | ||
} | ||
|
||
if time.Now().After(session.ExpiresAt) { | ||
err := fmt.Errorf("expired session") | ||
ctx.JSON(http.StatusUnauthorized, errorResponse(err)) | ||
return | ||
} | ||
|
||
accessToken, accessPayload, err := server.tokenMaker.CreateToken( | ||
refreshPayload.Username, | ||
server.config.AccessTokenDuration, | ||
) | ||
if err != nil { | ||
ctx.JSON(http.StatusInternalServerError, errorResponse(err)) | ||
return | ||
} | ||
|
||
rsp := renewAccessTokenResponse{ | ||
AccessToken: accessToken, | ||
AccessTokenExpiresAt: accessPayload.ExpiredAt, | ||
} | ||
ctx.JSON(http.StatusOK, rsp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE IF EXISTS "sessions"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
CREATE TABLE "sessions" ( | ||
"id" uuid PRIMARY KEY, | ||
"username" varchar NOT NULL, | ||
"refresh_token" varchar NOT NULL, | ||
"user_agent" varchar NOT NULL, | ||
"client_ip" varchar NOT NULL, | ||
"is_blocked" boolean NOT NULL DEFAULT false, | ||
"expires_at" timestamptz NOT NULL, | ||
"created_at" timestamptz NOT NULL DEFAULT (now()) | ||
); | ||
|
||
ALTER TABLE "sessions" ADD FOREIGN KEY ("username") REFERENCES "users" ("username"); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
-- name: CreateSession :one | ||
INSERT INTO sessions ( | ||
id, | ||
username, | ||
refresh_token, | ||
user_agent, | ||
client_ip, | ||
is_blocked, | ||
expires_at | ||
) VALUES ( | ||
$1, $2, $3, $4, $5, $6, $7 | ||
) RETURNING *; | ||
|
||
-- name: GetSession :one | ||
SELECT * FROM sessions | ||
WHERE id = $1 LIMIT 1; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.