forked from frankoe-dev/bukka_backend
-
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.
- Loading branch information
Showing
14 changed files
with
503 additions
and
5 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 |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
"Msgf", | ||
"pgtype", | ||
"pgxpool", | ||
"Timestamptz", | ||
"zerolog" | ||
] | ||
} |
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,20 @@ | ||
ALTER TABLE "password_reset_requests" DROP CONSTRAINT IF EXISTS "password_reset_requests_user_id_fkey"; | ||
ALTER TABLE "two_factor_secrets" DROP CONSTRAINT IF EXISTS "two_factor_secrets_user_id_fkey"; | ||
ALTER TABLE "two_factor_revocation" DROP CONSTRAINT IF EXISTS "two_factor_revocation_user_id_fkey"; | ||
ALTER TABLE "two_factor_backup_codes" DROP CONSTRAINT IF EXISTS "two_factor_backup_codes_user_id_fkey"; | ||
|
||
ALTER TABLE "account_recovery_requests" DROP CONSTRAINT IF EXISTS "account_recovery_requests_user_id_fkey"; | ||
|
||
|
||
-- Drop indexes | ||
DROP INDEX IF EXISTS "password_reset_requests_email_user_id_token_expires_at_idx"; | ||
DROP INDEX IF EXISTS "account_recovery_requests_user_id_idx"; | ||
|
||
|
||
-- Drop tables | ||
DROP TABLE IF EXISTS "two_factor_backup_codes"; | ||
DROP TABLE IF EXISTS "two_factor_revocation"; | ||
DROP TABLE IF EXISTS "two_factor_secrets"; | ||
DROP TABLE IF EXISTS "password_reset_requests"; | ||
|
||
DROP TABLE IF EXISTS "account_recovery_requests"; |
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,57 @@ | ||
CREATE TABLE "password_reset_requests" ( | ||
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, | ||
"user_id" uuid NOT NULL, | ||
"email" varchar NOT NULL, | ||
"token" varchar NOT NULL UNIQUE, | ||
"used" boolean DEFAULT false, | ||
"created_at" timestamptz DEFAULT (now()), | ||
"expires_at" timestamptz NOT NULL | ||
); | ||
|
||
CREATE TABLE "two_factor_secrets" ( | ||
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, | ||
"user_id" uuid NOT NULL, -- UNIQUE, | ||
"secret_key" varchar NOT NULL, | ||
"is_active" boolean DEFAULT true | ||
); | ||
|
||
CREATE TABLE "two_factor_revocation" ( | ||
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, | ||
"user_id" uuid NOT NULL, | ||
"revoked_at" timestamptz, | ||
"revocation_reason" varchar | ||
-- "revoked_by" UUID REFERENCES users(id) | ||
); | ||
|
||
CREATE TABLE "two_factor_backup_codes" ( | ||
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, | ||
"user_id" uuid NOT NULL, | ||
"code" varchar NOT NULL, | ||
"used" boolean DEFAULT false | ||
); | ||
|
||
CREATE TABLE "account_recovery_requests" ( | ||
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, | ||
"user_id" uuid NOT NULL, | ||
"email" varchar NOT NULL, | ||
"used" BOOLEAN DEFAULT false, | ||
"recovery_token" varchar unique not null, | ||
"requested_at" timestamptz DEFAULT (now()), | ||
"expires_at" timestamptz not null, | ||
"completed_at" timestamptz | ||
); | ||
|
||
CREATE INDEX ON "password_reset_requests" ( "user_id","email", "token", "expires_at"); | ||
|
||
CREATE INDEX ON "account_recovery_requests" ( "user_id", "recovery_token"); | ||
|
||
|
||
ALTER TABLE "password_reset_requests" ADD FOREIGN KEY ("user_id") REFERENCES "authentications" ("id"); | ||
|
||
ALTER TABLE "two_factor_secrets" ADD FOREIGN KEY ("user_id") REFERENCES "authentications" ("id"); | ||
|
||
ALTER TABLE "two_factor_revocation" ADD FOREIGN KEY ("user_id") REFERENCES "authentications" ("id"); | ||
|
||
ALTER TABLE "two_factor_backup_codes" ADD FOREIGN KEY ("user_id") REFERENCES "authentications" ("id"); | ||
|
||
ALTER TABLE "account_recovery_requests" ADD FOREIGN KEY ("user_id") REFERENCES "authentications" ("id"); |
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,11 @@ | ||
-- name: CreateUserDeleteRequest :exec | ||
INSERT INTO account_recovery_requests ( | ||
user_id, email, recovery_token, expires_at, completed_at | ||
) | ||
VALUES ($1, $2, $3, $4, $5); | ||
|
||
-- name: GetUserFromDeleteReqByToken :one | ||
SELECT * FROM account_recovery_requests WHERE recovery_token = $1 LIMIT 1; | ||
|
||
-- name: MarkDeleteAsUsedByToken :exec | ||
UPDATE account_recovery_requests SET used = true, completed_at = now() WHERE recovery_token = $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.
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,64 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/steve-mir/bukka_backend/internal/app/auth/middlewares" | ||
"github.com/steve-mir/bukka_backend/internal/app/auth/services" | ||
"github.com/steve-mir/bukka_backend/token" | ||
) | ||
|
||
func (s *Server) deleteAccount(ctx *gin.Context) { | ||
authPayload := ctx.MustGet(middlewares.AuthorizationPayloadKey).(*token.Payload) | ||
|
||
var req services.DeleteAccountReq | ||
if err := ctx.ShouldBindJSON(&req); err != nil { | ||
ctx.JSON(http.StatusBadRequest, errorResponse(err)) | ||
return | ||
} | ||
|
||
err := services.DeleteAccountRequest(ctx, req.Password, s.store, authPayload.Subject) | ||
if err != nil { | ||
ctx.JSON(http.StatusBadRequest, errorResponse(err)) | ||
return | ||
} | ||
|
||
ctx.JSON(http.StatusOK, services.GenericRes{ | ||
Msg: "Account deleted successfully", | ||
}) | ||
|
||
} | ||
|
||
func (s *Server) requestAccountRecovery(ctx *gin.Context) { | ||
var req services.AccountRecoveryReq | ||
if err := ctx.ShouldBindJSON(&req); err != nil { | ||
ctx.JSON(http.StatusBadRequest, errorResponse(err)) | ||
return | ||
} | ||
|
||
err := services.AccRecoveryRequest(ctx, s.store, s.taskDistributor, req.Email) | ||
if err != nil { | ||
ctx.JSON(http.StatusBadRequest, errorResponse(err)) | ||
return | ||
} | ||
|
||
ctx.JSON(http.StatusOK, services.GenericRes{ | ||
Msg: "URL has been sent to your email", | ||
}) | ||
|
||
} | ||
|
||
func (s *Server) completeAccountRecovery(ctx *gin.Context) { | ||
token := ctx.Query("token") | ||
err := services.AccountRecovery(ctx, s.connPool, s.store, token) | ||
if err != nil { | ||
ctx.JSON(http.StatusBadRequest, errorResponse(err)) | ||
return | ||
} | ||
|
||
ctx.JSON(http.StatusOK, services.GenericRes{ | ||
Msg: "Account recovered, you can now login to access your account.", | ||
}) | ||
|
||
} |
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
Oops, something went wrong.