Skip to content

Commit a856e9c

Browse files
committed
feat: basic session auth implemented
1 parent 558ed99 commit a856e9c

File tree

12 files changed

+328
-211
lines changed

12 files changed

+328
-211
lines changed

swiftwave_service/core/models.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,21 @@ type ServerLog struct {
3737

3838
// User hold information about user
3939
type User struct {
40-
ID uint `json:"id" gorm:"primaryKey"`
41-
Username string `json:"username" gorm:"unique"`
42-
Role UserRole `json:"role" gorm:"default:'user'"`
43-
PasswordHash string `json:"password_hash"`
44-
TotpEnabled bool `json:"totp_enabled" gorm:"default:false"`
45-
TotpSecret string `json:"totp_secret"`
40+
ID uint `json:"id" gorm:"primaryKey"`
41+
Username string `json:"username" gorm:"unique"`
42+
Role UserRole `json:"role" gorm:"default:'user'"`
43+
PasswordHash string `json:"password_hash"`
44+
TotpEnabled bool `json:"totp_enabled" gorm:"default:false"`
45+
TotpSecret string `json:"totp_secret"`
46+
Sessions []UserSession `json:"sessions" gorm:"foreignKey:UserID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
47+
}
48+
49+
// UserSession hold information about
50+
type UserSession struct {
51+
ID uint `json:"id" gorm:"primaryKey"`
52+
UserID uint `json:"user_id"`
53+
SessionID string `json:"session_id" gorm:"index"`
54+
ExpiresAt time.Time `json:"expires_at"`
4655
}
4756

4857
// ************************************************************************************* //
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package core
2+
3+
import (
4+
"context"
5+
"errors"
6+
"github.com/labstack/gommon/random"
7+
"gorm.io/gorm"
8+
"time"
9+
)
10+
11+
// This file contains the operations for the User Sessions model.
12+
// This functions will perform necessary validation before doing the actual database operation.
13+
14+
// Each function's argument format should be (ctx context.Context, db gorm.DB, ...)
15+
// context used to pass some data to the function e.g. user id, auth info, etc.
16+
17+
// CreateSession : create session for user
18+
func CreateSession(ctx context.Context, db gorm.DB, user User) (string, error) {
19+
sessionRecord := &UserSession{
20+
UserID: user.ID,
21+
SessionID: random.String(128),
22+
ExpiresAt: time.Now().Add(time.Hour * 720),
23+
}
24+
// Create record
25+
err := db.Create(sessionRecord).Error
26+
if err != nil {
27+
return "", err
28+
}
29+
return sessionRecord.SessionID, nil
30+
}
31+
32+
// GetUserIDBySessionID : get user by session id
33+
func GetUserIDBySessionID(ctx context.Context, db gorm.DB, sessionID string) (uint, error) {
34+
var session UserSession
35+
err := db.Where("session_id = ?", sessionID).Select("user_id").First(&session).Error
36+
if err != nil {
37+
return 0, errors.New("invalid session")
38+
}
39+
return session.UserID, nil
40+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- reverse: create index "idx_user_sessions_session_id" to table: "user_sessions"
2+
DROP INDEX "public"."idx_user_sessions_session_id";
3+
-- reverse: create "user_sessions" table
4+
DROP TABLE "public"."user_sessions";
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- create "user_sessions" table
2+
CREATE TABLE "public"."user_sessions" (
3+
"id" bigserial NOT NULL,
4+
"user_id" bigint NULL,
5+
"session_id" text NULL,
6+
"expires_at" timestamptz NULL,
7+
PRIMARY KEY ("id"),
8+
CONSTRAINT "fk_users_sessions" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("id") ON UPDATE CASCADE ON DELETE CASCADE
9+
);
10+
-- create index "idx_user_sessions_session_id" to table: "user_sessions"
11+
CREATE INDEX "idx_user_sessions_session_id" ON "public"."user_sessions" ("session_id");

swiftwave_service/db/migrations/atlas.sum

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
h1:dFMxl02tstPc5Nmao8FA3S7sM7mIXqsSy34VElMP2gk=
1+
h1:SwRv04ZCBJM2kVRlT9CroRjbfMlf4sOror5mPG2X/EM=
22
20240413191732_init.down.sql h1:HoitObGwuKF/akF4qg3dol2FfNTLCEuf6wHYDuCez8I=
33
20240413191732_init.up.sql h1:USKdQx/yTz1KJ0+mDwYGhKm3WzX7k+I9+6B6SxImwaE=
44
20240414051823_server_custom_ssh_port_added.down.sql h1:IC1DFQBQceTPTRdZOo5/WqytH+ZbgcKrQuMCkhArF/0=
@@ -47,3 +47,5 @@ h1:dFMxl02tstPc5Nmao8FA3S7sM7mIXqsSy34VElMP2gk=
4747
20240628175617_add_extra_fields_in_app_group.up.sql h1:+qBOQc/2bhG1igFdUbWSsZEy01aORuPyBPvxKVXJJoA=
4848
20240906153014_add_hostname_in_application.down.sql h1:tFY94wo3G+UYA51UYt9jbfwwPTS/hCd3pG2F7smUEB8=
4949
20240906153014_add_hostname_in_application.up.sql h1:JAhs73vgSIUzt0l8M8ltRp98dVkwL5lXrdkfHvJ+arE=
50+
20250213190430_test.down.sql h1:ra8BJ92iaL0/Kc7MThF+wzbM1/szBVKHxJUWLq1hf5o=
51+
20250213190430_test.up.sql h1:EDgRcbJknAyddUQ9X3+uGw/MNDilVWHRKcC+9Xuxuxg=

swiftwave_service/db_models_loader/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func main() {
1717
&core.Server{},
1818
&core.ServerLog{},
1919
&core.User{},
20+
&core.UserSession{},
2021
&core.Domain{},
2122
&core.RedirectRule{},
2223
&core.PersistentVolume{},

swiftwave_service/graphql/authentication.resolvers.go

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

swiftwave_service/graphql/generated.go

Lines changed: 133 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

swiftwave_service/graphql/model/models_gen.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
input UserCredential {
2+
username: String!
3+
password: String!
4+
}
5+
6+
7+
extend type Mutation {
8+
login(input: UserCredential!): String!
9+
}

0 commit comments

Comments
 (0)