Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
uvulpos committed Sep 23, 2024
1 parent 66901ca commit 8de4340
Show file tree
Hide file tree
Showing 28 changed files with 131 additions and 99 deletions.
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ APP_WEBSERVER_SHOW_FRONTEND=1
APP_WEBSERVER_SHOW_SWAGGER=1
APP_WEBSERVER_ENABLE_SWAGGER_DEV_EP=1

APP_DATABASE_ADDR="postgres"
# APP_DATABASE_ADDR="postgres"
APP_DATABASE_ADDR="127.0.0.1"
APP_DATABASE_PORT=5432
APP_DATABASE_SSL=0
APP_DATABASE_USERNAME="postgres"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
package user

import (
"encoding/json"
"io"
"net/http"
"testing"

"github.com/uvulpos/golang-sveltekit-template/integration-tests/helper/setup"
"gotest.tools/assert"

httpUserModel "github.com/uvulpos/golang-sveltekit-template/src/resources/user/http/http-models"
)

func TestGetUserPermissions(t *testing.T) {
testData := setup.SetupTest(t)
testData.AuthenticateAsUser(t, "")
testData.AuthenticateAsUser(t, "TIRIEDL")

response := testData.MakeRequest(t, http.MethodGet, "/api/v1/self/permissions", nil)
response := testData.MakeRequest(t, http.MethodGet, "/api/v1/self", nil)
defer response.Body.Close()

assert.Equal(t, "", "", "")
responseBytes, responseBytesErr := io.ReadAll(response.Body)
assert.Equal(t, responseBytesErr, nil, "could not read body of http request")

var selfUserInformation httpUserModel.SelfInformationModel
unmarshalErr := json.Unmarshal(responseBytes, &selfUserInformation)
assert.Equal(t, unmarshalErr, nil, "could not unmarshal http request")

assert.Equal(t, selfUserInformation.Username, "TIRIEDL", "blabla")
}
3 changes: 2 additions & 1 deletion services/backend/integration-tests/helper/setup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/gofiber/fiber/v2"
"github.com/uvulpos/golang-sveltekit-template/src/configuration"
jwtService "github.com/uvulpos/golang-sveltekit-template/src/resources/jwt/service"
webApp "github.com/uvulpos/golang-sveltekit-template/src/web-app"
customhttphandler "github.com/uvulpos/golang-sveltekit-template/src/web-app/custom-http-handler"
Expand Down Expand Up @@ -69,7 +70,7 @@ func (s *TestSetupStruct) MakeRequest(t *testing.T, applicationMethod, applicati
req.AddCookie(cookie)
}

response, responseErr := s.FiberRouter.Test(req, 1)
response, responseErr := s.FiberRouter.Test(req, configuration.INTEGRATION_TEST_TEST_TIMEOUT_IN_MS)
assert.Equal(t, responseErr, nil, "")

return response
Expand Down
2 changes: 2 additions & 0 deletions services/backend/src/configuration/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ const (
CONST_APPLICATION_NAME = "Go + Svelte App"
CONST_APPLICATION_PATH_NAME = "gosvelte-app"
CONST_APPLICATION_BRANDING_HEADER = "Example Application by @uvulpos"

INTEGRATION_TEST_TEST_TIMEOUT_IN_MS = 5000
)
4 changes: 2 additions & 2 deletions services/backend/src/configuration/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ var (
WEBSERVER_SHOW_SWAGGER = GetEnvOrDefaultBool("WEBSERVER_SHOW_SWAGGER", true)

// Database
// DATABASE_ADDR = GetEnvOrDefaultString("DATABASE_ADDR", "127.0.0.1")
DATABASE_ADDR = GetEnvOrDefaultString("DATABASE_ADDR", "postgres")
DATABASE_ADDR = GetEnvOrDefaultString("DATABASE_ADDR", "127.0.0.1")
// DATABASE_ADDR = GetEnvOrDefaultString("DATABASE_ADDR", "postgres")
DATABASE_PORT = GetEnvOrDefaultInt("DATABASE_PORT", 5432)
DATABASE_SSL = GetEnvOrDefaultBool("DATABASE_SSL", false)
DATABASE_USERNAME = GetEnvOrDefaultString("DATABASE_USERNAME", "postgres")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/go-sqlx/sqlx"
"github.com/uvulpos/golang-sveltekit-template/src/helper/customerrors"
customerrorconst "github.com/uvulpos/golang-sveltekit-template/src/helper/customerrors/custom-error-const"
providerConst "github.com/uvulpos/golang-sveltekit-template/src/resources/auth/service/provider-const"
)

func (s *AuthService) AuthentikCallbackFunction(authCode, state string) (string, customerrors.ErrorInterface) {
Expand All @@ -35,15 +36,15 @@ func (s *AuthService) AuthentikCallbackFunction(authCode, state string) (string,
return "", customerrors.NewInternalServerError(err, "", "(oauth callback authentik) Failed to unmarshal authentik oauth user response body")
}

loginUserID, loginUserError := s.storage.GetUserIDByLogin("Authentik", result.ID)
loginUserID, loginUserError := s.userSvc.GetUserIDByLogin(providerConst.Authentik, result.ID)
if loginUserError != nil {

if loginUserError.ErrorType() != customerrorconst.ERROR_IDENTIFIER_DATABASE_NOT_FOUND {
return "", loginUserError
}

// Create new user if user does not exist or relationship cannot be established
tx, txErr := s.storage.StartTransaction()
tx, txErr := s.userSvc.StartTransaction()
if txErr != nil {
return "", txErr
}
Expand All @@ -52,7 +53,7 @@ func (s *AuthService) AuthentikCallbackFunction(authCode, state string) (string,
tx.Rollback()
}(tx)

createdUserID, createUserErr := s.storage.CreateUser(
createdUserID, createUserErr := s.userSvc.CreateUser(
tx,
result.Name,
result.PreferredUsername,
Expand All @@ -66,10 +67,10 @@ func (s *AuthService) AuthentikCallbackFunction(authCode, state string) (string,

loginUserID = createdUserID

createdUserLoginIdentityErr := s.storage.CreateUserLoginIdentity(
createdUserLoginIdentityErr := s.userSvc.CreateUserLoginIdentity(
tx,
createdUserID,
"Authentik",
providerConst.Authentik,
result.ID,
)
if createdUserLoginIdentityErr != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,42 @@ package service

import (
"github.com/go-sqlx/sqlx"
"github.com/uvulpos/golang-sveltekit-template/src/configuration"
"github.com/uvulpos/golang-sveltekit-template/src/helper/customerrors"
"golang.org/x/oauth2"

userService "github.com/uvulpos/golang-sveltekit-template/src/resources/user/service"
)

type AuthService struct {
authentikConfig *oauth2.Config
authentikOauthUserInfoEP string
authentikOauthLogoutEP string

storage AuthStorageInterface
userSvc *userService.UserService
}

func NewAuthService(storage AuthStorageInterface, OAuthKey, OAuthSecret, CallbackURL, AuthURL, AuthTokenURL, UserInfoURL, LogoutURL string, scope ...string) *AuthService {
func NewAuthService(userSvc *userService.UserService) *AuthService {
authentikConfig := &oauth2.Config{
ClientID: OAuthKey,
ClientSecret: OAuthSecret,
ClientID: configuration.AUTHORIZATION_OAUTH_KEY,
ClientSecret: configuration.AUTHORIZATION_OAUTH_SECRET,

RedirectURL: CallbackURL,
Scopes: scope,
RedirectURL: configuration.AUTHORIZATION_OAUTH_CALLBACK_URL,
Scopes: configuration.AUTHORIZATION_OAUTH_SCOPES,

Endpoint: oauth2.Endpoint{
AuthURL: AuthURL,
TokenURL: AuthTokenURL,
AuthURL: configuration.AUTHORIZATION_OAUTH_AUTHORIZATION_URL,
TokenURL: configuration.AUTHORIZATION_OAUTH_TOKEN_URL,

AuthStyle: oauth2.AuthStyleInParams,
},
}
return &AuthService{
authentikConfig: authentikConfig,
authentikOauthUserInfoEP: UserInfoURL,
authentikOauthLogoutEP: LogoutURL,
authentikOauthUserInfoEP: configuration.AUTHORIZATION_OAUTH_USERINFO_URL,
authentikOauthLogoutEP: configuration.AUTHORIZATION_OAUTH_LOGOUT_URL,

storage: storage,
userSvc: userSvc,
}
}

Expand Down
10 changes: 1 addition & 9 deletions services/backend/src/resources/auth/service/callback.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package service

import (
"fmt"

"github.com/uvulpos/golang-sveltekit-template/src/helper/customerrors"
jwtService "github.com/uvulpos/golang-sveltekit-template/src/resources/jwt/service"
)

func (s *AuthService) CallbackFunction(authCode, state, ipaddr, userAgent string) (string, string, customerrors.ErrorInterface) {
loggedinUser, loggedinUserErr := s.auth.AuthentikCallbackFunction(authCode, state)
loggedinUser, loggedinUserErr := s.authentikProviderSvc.AuthentikCallbackFunction(authCode, state)
if loggedinUserErr != nil {
return "", "", loggedinUserErr
}
Expand Down Expand Up @@ -40,12 +38,6 @@ func (s *AuthService) CallbackFunction(authCode, state, ipaddr, userAgent string
return "", "", permissionScopesErr
}

fmt.Println("Permissions")
fmt.Println("Permissions")
fmt.Println("Permissions")
fmt.Println("Permissions")
fmt.Println(permissionScopes)

commitErr := tx.Commit()
if commitErr != nil {
return "", "", customerrors.NewDatabaseTransactionCommitError(commitErr, "Failed to commit transaction")
Expand Down
12 changes: 8 additions & 4 deletions services/backend/src/resources/auth/service/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@ package service
import (
"github.com/go-sqlx/sqlx"
"github.com/uvulpos/golang-sveltekit-template/src/helper/customerrors"
authService "github.com/uvulpos/golang-sveltekit-template/src/resources/identity-provider/auth/service"
authentikProviderService "github.com/uvulpos/golang-sveltekit-template/src/resources/auth/service/authentik"
jwtService "github.com/uvulpos/golang-sveltekit-template/src/resources/jwt/service"
userService "github.com/uvulpos/golang-sveltekit-template/src/resources/user/service"
)

type AuthService struct {
storage AuthStorageInterface

auth *authService.AuthService
jwt *jwtService.JwtService
userSvc *userService.UserService

authentikProviderSvc *authentikProviderService.AuthService
}

func NewAuthService(storage AuthStorageInterface, authService *authService.AuthService, jwtService *jwtService.JwtService, userService *userService.UserService) *AuthService {
func NewAuthService(storage AuthStorageInterface, jwtService *jwtService.JwtService, userService *userService.UserService) *AuthService {
authentikProviderSvc := authentikProviderService.NewAuthService(userService)

return &AuthService{
storage: storage,
auth: authService,
jwt: jwtService,
userSvc: userService,

authentikProviderSvc: authentikProviderSvc,
}
}

Expand Down
2 changes: 1 addition & 1 deletion services/backend/src/resources/auth/service/logout.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package service

func (s *AuthService) Logout() (string, error) {
return s.auth.Logout()
return s.authentikProviderSvc.Logout()
}
2 changes: 1 addition & 1 deletion services/backend/src/resources/auth/service/redirect.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package service

func (s *AuthService) CreateRedirect(state string) string {
return s.auth.CreateRedirect(state)
return s.authentikProviderSvc.CreateRedirect(state)
}

This file was deleted.

10 changes: 10 additions & 0 deletions services/backend/src/resources/user/service/create_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package service

import (
"github.com/go-sqlx/sqlx"
"github.com/uvulpos/golang-sveltekit-template/src/helper/customerrors"
)

func (s *UserService) CreateUser(tx *sqlx.Tx, name, prefName, email string, emailVerified bool) (string, customerrors.ErrorInterface) {
return "ID", nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package service

import (
"github.com/go-sqlx/sqlx"
"github.com/uvulpos/golang-sveltekit-template/src/helper/customerrors"
providerConst "github.com/uvulpos/golang-sveltekit-template/src/resources/auth/service/provider-const"
)

func (*UserService) CreateUserLoginIdentity(
tx *sqlx.Tx,
createdUserID string,
providerType providerConst.AuthProvider,
providerID string,
) customerrors.ErrorInterface {
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package service

import (
"errors"

"github.com/uvulpos/golang-sveltekit-template/src/helper/customerrors"
providerConst "github.com/uvulpos/golang-sveltekit-template/src/resources/auth/service/provider-const"
)

func (s *UserService) GetUserIDByLogin(providerType providerConst.AuthProvider, providerID string) (string, customerrors.ErrorInterface) {
return "", customerrors.NewInternalServerError(errors.New("function not implemented"), "", "function not implemented")
}
4 changes: 4 additions & 0 deletions services/backend/src/resources/user/service/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ type AuthStorageInterface interface {
GetUserByUsername(tx *sqlx.Tx, username string) (*serviceModel.UserModel, customerrors.ErrorInterface)
GetUserAuthSessionByID(tx *sqlx.Tx, sessionID string) (*serviceModel.SessionModel, customerrors.ErrorInterface)
}

func (s *UserService) StartTransaction() (*sqlx.Tx, customerrors.ErrorInterface) {
return s.storage.StartTransaction()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/uvulpos/golang-sveltekit-template/src/helper/customerrors"
)

func (s *AuthStorage) CreateUser(tx *sqlx.Tx, displayName string, username string, email string, emailVerified bool) (string, customerrors.ErrorInterface) {
func (s *UserStore) CreateUser(tx *sqlx.Tx, displayName string, username string, email string, emailVerified bool) (string, customerrors.ErrorInterface) {
sqlquery := "INSERT INTO public.users (display_name, username, email, email_verified) VALUES ($1, $2, $3, $4) RETURNING id"

var userID string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/uvulpos/golang-sveltekit-template/src/helper/customerrors"
)

func (s *AuthStorage) CreateUserLoginIdentity(tx *sqlx.Tx, createdUserID string, authProvider string, authProviderID string) customerrors.ErrorInterface {
func (s *UserStore) CreateUserLoginIdentity(tx *sqlx.Tx, createdUserID string, authProvider string, authProviderID string) customerrors.ErrorInterface {
sqlquery := "INSERT INTO public.user_identities (provider, provider_user_id, user_id) VALUES ($1, $2, $3)"

_, err := tx.Exec(sqlquery, authProvider, authProviderID, createdUserID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/uvulpos/golang-sveltekit-template/src/helper/customerrors"
)

func (s *AuthStorage) GetUserIDByLogin(provider string, providerID string) (string, customerrors.ErrorInterface) {
func (s *UserStore) GetUserIDByLogin(provider string, providerID string) (string, customerrors.ErrorInterface) {
var userID string
const sqlquery = "SELECT user_id FROM public.user_identities WHERE provider=$1 AND provider_user_id=$2"

Expand Down
Loading

0 comments on commit 8de4340

Please sign in to comment.