-
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.
Merge pull request #12 from imrishuroy/ft/user-session
added user sessions
- Loading branch information
Showing
19 changed files
with
347 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,46 @@ | ||
# name: Deploy to production | ||
name: Deploy to production | ||
|
||
# on: | ||
# push: | ||
# branches: [ main ] | ||
# # pull_request: | ||
# # branches: [ main ] | ||
on: | ||
push: | ||
branches: [ prod ] | ||
# pull_request: | ||
# branches: [ main ] | ||
|
||
# jobs: | ||
# deploy: | ||
# name: Deploy | ||
# runs-on: ubuntu-latest | ||
jobs: | ||
deploy: | ||
name: Deploy | ||
runs-on: ubuntu-latest | ||
|
||
# steps: | ||
# - name: Checkout code | ||
# uses: actions/checkout@v2 | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
# - name: Configure AWS credentials | ||
# uses: aws-actions/configure-aws-credentials@v1 | ||
# with: | ||
# aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | ||
# aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | ||
# aws-region: ap-south-1 | ||
- name: Configure AWS credentials | ||
uses: aws-actions/configure-aws-credentials@v1 | ||
with: | ||
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | ||
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | ||
aws-region: ap-south-1 | ||
|
||
# - name: Login to Amazon ECR | ||
# id: login-ecr | ||
# uses: aws-actions/amazon-ecr-login@v1 | ||
- name: Login to Amazon ECR | ||
id: login-ecr | ||
uses: aws-actions/amazon-ecr-login@v1 | ||
|
||
# - name: Load secrets and save to app.env | ||
# run: aws secretsmanager get-secret-value --secret-id simple_bank --query SecretString --output text | jq -r 'to_entries|map("\(.key)=\(.value)")|.[]' > app.env | ||
- name: Load secrets and save to app.env | ||
run: aws secretsmanager get-secret-value --secret-id simple_bank --query SecretString --output text | jq -r 'to_entries|map("\(.key)=\(.value)")|.[]' > app.env | ||
|
||
# - name: Build, tag, and push the image to Amazon ECR | ||
# id: build-image | ||
# env: | ||
# ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | ||
# ECR_REPOSITORY: simplebank | ||
# IMAGE_TAG: latest | ||
# run: | | ||
# # Build a docker container and push it to ECR | ||
# docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . | ||
# echo "Pushing image to ECR..." | ||
# docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | ||
# echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" | ||
- name: Build, tag, and push the image to Amazon ECR | ||
id: build-image | ||
env: | ||
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | ||
ECR_REPOSITORY: simplebank | ||
IMAGE_TAG: latest | ||
run: | | ||
# Build a docker container and push it to ECR | ||
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . | ||
echo "Pushing image to ECR..." | ||
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | ||
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" | ||
|
||
|
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
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.
Oops, something went wrong.