-
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.
⚡️ Added session.CreateToken function
- Loading branch information
1 parent
ee0abfd
commit 4c9f2b6
Showing
8 changed files
with
121 additions
and
4 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
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,43 @@ | ||
package session | ||
|
||
import ( | ||
"context" | ||
"crypto/hmac" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"ahbcc/internal/log" | ||
) | ||
|
||
// CreateToken creates a new session token for the user login action | ||
type CreateToken func(ctx context.Context, userID int) (string, time.Time, error) | ||
|
||
// MakeCreateToken creates a new CreateToken function | ||
func MakeCreateToken(insertUserSession Insert) CreateToken { | ||
return func(ctx context.Context, userID int) (string, time.Time, error) { | ||
expiresAt := time.Now().Add(30 * 24 * time.Hour) // 30 days session expiry | ||
payload := fmt.Sprintf("%d:%d", userID, expiresAt.Unix()) | ||
|
||
mac := hmac.New(sha256.New, []byte(os.Getenv("SESSION_SECRET_KEY"))) | ||
mac.Write([]byte(payload)) | ||
signature := base64.URLEncoding.EncodeToString(mac.Sum(nil)) | ||
token := base64.URLEncoding.EncodeToString([]byte(payload)) + "." + signature | ||
|
||
session := DAO{ | ||
UserID: userID, | ||
Token: token, | ||
ExpiresAt: expiresAt, | ||
} | ||
|
||
err := insertUserSession(ctx, session) | ||
if err != nil { | ||
log.Error(ctx, err.Error()) | ||
return "", time.Time{}, FailedToCreatUserSessionToken | ||
} | ||
|
||
return token, expiresAt, nil | ||
} | ||
} |
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,41 @@ | ||
package session_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"os" | ||
"testing" | ||
|
||
"github.com/joho/godotenv" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"ahbcc/cmd/api/user/session" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
_ = godotenv.Load() | ||
os.Exit(m.Run()) | ||
} | ||
|
||
func TestCreateToken_success(t *testing.T) { | ||
mockInsertUserSession := session.MockInsertUserSession(nil) | ||
|
||
createSessionToken := session.MakeCreateToken(mockInsertUserSession) | ||
|
||
token, createdAt, got := createSessionToken(context.Background(), 1) | ||
|
||
assert.Nil(t, got) | ||
assert.NotNil(t, createdAt) | ||
assert.NotNil(t, token) | ||
} | ||
|
||
func TestCreateToken_failsWhenInsertUserSessionThrowsError(t *testing.T) { | ||
mockInsertUserSession := session.MockInsertUserSession(errors.New("failed to insert user session")) | ||
|
||
createSessionToken := session.MakeCreateToken(mockInsertUserSession) | ||
|
||
want := session.FailedToCreatUserSessionToken | ||
_, _, got := createSessionToken(context.Background(), 1) | ||
|
||
assert.Equal(t, want, got) | ||
} |
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