-
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 #88 from fabiante/feat/user-keys
- Loading branch information
Showing
11 changed files
with
175 additions
and
18 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package api | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/fabiante/persurl/app" | ||
"github.com/fabiante/persurl/app/models" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
const ( | ||
headerToken = "Persurl-Key" | ||
) | ||
|
||
func authenticatedMiddleware(service app.UserServiceInterface) gin.HandlerFunc { | ||
return func(ctx *gin.Context) { | ||
token := ctx.GetHeader(headerToken) | ||
if token == "" { | ||
respondWithError(ctx, http.StatusUnauthorized, errors.New("unauthorized")) | ||
return | ||
} | ||
|
||
// load user by email | ||
user, err := service.GetUserByKey(token) | ||
switch { | ||
case errors.Is(err, app.ErrNotFound): | ||
respondWithError(ctx, http.StatusUnauthorized, errors.New("user not found")) | ||
return | ||
case err != nil: | ||
respondWithError(ctx, http.StatusInternalServerError, err) | ||
return | ||
} | ||
|
||
ctx.Set("user", user) | ||
|
||
ctx.Next() | ||
} | ||
} | ||
|
||
func getAuthenticatedUser(ctx *gin.Context) *models.User { | ||
value, exists := ctx.Get("user") | ||
if !exists { | ||
panic(fmt.Errorf("missing context value for key 'user'. ensure you use the appropriate middleware on this endpoint")) | ||
} | ||
|
||
typed, ok := value.(*models.User) | ||
if !ok { | ||
panic(fmt.Errorf("context value has unexpected type %T", value)) | ||
} | ||
|
||
return typed | ||
} |
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
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 |
---|---|---|
|
@@ -3,6 +3,8 @@ package dsl | |
import ( | ||
"testing" | ||
|
||
"github.com/fabiante/persurl/app" | ||
"github.com/fabiante/persurl/app/models" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
|
@@ -22,3 +24,12 @@ func GivenExistingDomain(t *testing.T, service AdminAPI, domain string) { | |
err := service.CreateDomain(domain) | ||
require.NoError(t, err, "creating domain failed") | ||
} | ||
|
||
// GivenSomeUser creates a user and returns the key for it. | ||
func GivenSomeUser(t *testing.T, userService *app.UserService) *models.UserKey { | ||
user, err := userService.CreateUser("[email protected]") | ||
require.NoError(t, err) | ||
|
||
key, err := userService.CreateUserKey(user) | ||
return key | ||
} |
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