-
-
Notifications
You must be signed in to change notification settings - Fork 7
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
202 additions
and
3 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 |
---|---|---|
|
@@ -33,6 +33,10 @@ Content-Type: application/json | |
|
||
### | ||
|
||
PUT {{url}}/profile/key | ||
|
||
### | ||
|
||
GET {{url}}/targets | ||
|
||
### | ||
|
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package refresh_api_key | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/YuukanOO/seelf/internal/auth/domain" | ||
"github.com/YuukanOO/seelf/pkg/bus" | ||
) | ||
|
||
type Command struct { | ||
bus.Command[string] | ||
|
||
ID string `json:"-"` | ||
} | ||
|
||
func (Command) Name_() string { return "auth.command.refresh_api_key" } | ||
|
||
func Handler( | ||
reader domain.UsersReader, | ||
writer domain.UsersWriter, | ||
generator domain.KeyGenerator, | ||
) bus.RequestHandler[string, Command] { | ||
return func(ctx context.Context, cmd Command) (string, error) { | ||
user, err := reader.GetByID(ctx, domain.UserID(cmd.ID)) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
key, err := generator.Generate() | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
user.HasAPIKey(key) | ||
|
||
if err = writer.Write(ctx, &user); err != nil { | ||
return "", err | ||
} | ||
|
||
return string(key), 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,48 @@ | ||
package refresh_api_key_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/YuukanOO/seelf/internal/auth/app/refresh_api_key" | ||
"github.com/YuukanOO/seelf/internal/auth/domain" | ||
"github.com/YuukanOO/seelf/internal/auth/infra/crypto" | ||
"github.com/YuukanOO/seelf/internal/auth/infra/memory" | ||
"github.com/YuukanOO/seelf/pkg/apperr" | ||
"github.com/YuukanOO/seelf/pkg/bus" | ||
"github.com/YuukanOO/seelf/pkg/must" | ||
"github.com/YuukanOO/seelf/pkg/testutil" | ||
) | ||
|
||
func Test_RefreshApiKey(t *testing.T) { | ||
sut := func(existingUsers ...*domain.User) bus.RequestHandler[string, refresh_api_key.Command] { | ||
store := memory.NewUsersStore(existingUsers...) | ||
|
||
return refresh_api_key.Handler(store, store, crypto.NewKeyGenerator()) | ||
} | ||
|
||
t.Run("should fail if the user does not exists", func(t *testing.T) { | ||
uc := sut() | ||
|
||
_, err := uc(context.Background(), refresh_api_key.Command{}) | ||
|
||
testutil.ErrorIs(t, apperr.ErrNotFound, err) | ||
}) | ||
|
||
t.Run("should refresh the user's API key if everything is good", func(t *testing.T) { | ||
user := must.Panic(domain.NewUser(domain.NewEmailRequirement("[email protected]", true), "someHashedPassword", "apikey")) | ||
uc := sut(&user) | ||
|
||
key, err := uc(context.Background(), refresh_api_key.Command{ | ||
ID: string(user.ID())}, | ||
) | ||
|
||
testutil.IsNil(t, err) | ||
testutil.NotEquals(t, "", key) | ||
|
||
evt := testutil.EventIs[domain.UserAPIKeyChanged](t, &user, 1) | ||
|
||
testutil.Equals(t, user.ID(), evt.ID) | ||
testutil.Equals(t, key, string(evt.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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,4 +65,16 @@ func Test_User(t *testing.T) { | |
testutil.Equals(t, u.ID(), evt.ID) | ||
testutil.Equals(t, "anotherPassword", evt.Password) | ||
}) | ||
|
||
t.Run("should be able to change API key", func(t *testing.T) { | ||
u := must.Panic(domain.NewUser(domain.NewEmailRequirement("[email protected]", true), "someHashedPassword", "apikey")) | ||
|
||
u.HasAPIKey("apikey") // no change, should not trigger events | ||
u.HasAPIKey("anotherKey") | ||
|
||
testutil.HasNEvents(t, &u, 2) | ||
evt := testutil.EventIs[domain.UserAPIKeyChanged](t, &u, 1) | ||
testutil.Equals(t, u.ID(), evt.ID) | ||
testutil.Equals(t, "anotherKey", evt.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
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