diff --git a/services/auth-app/README.md b/services/auth-app/README.md index 96a16e34fb..3dbf046ec6 100644 --- a/services/auth-app/README.md +++ b/services/auth-app/README.md @@ -40,8 +40,13 @@ The `auth-app` service provides an API to create (POST), list (GET) and delete ( Example: `expiry=72h` + Optionally, a `label` key/value pair to name the token. It is shown when listing the tokens and + helps to tell them apart. Without it, the label is `Generated via API`. + + Example: `label=WebDAV%20sync` + ```bash - curl --request POST 'https:///auth-app/tokens?expiry={value}' \ + curl --request POST 'https:///auth-app/tokens?expiry={value}&label={value}' \ --header 'accept: application/json' ``` @@ -78,13 +83,13 @@ The `auth-app` service provides an API to create (POST), list (GET) and delete ( "token": "155f402e-1c5c-411c-92d4-92f3b612cd99" "expiration_date": "2024-08-08T13:44:31.025199075+02:00", "created_date": "2024-08-07T13:44:31+02:00", - "label": "Generated via Impersonation API" + "label": "Generated via API (Impersonation)" }, { "token": "8c606bdb-e22e-4094-9304-732fd4702bc9" "expiration_date": "2024-08-08T13:46:41.936052281+02:00", "created_date": "2024-08-07T13:46:42+02:00", - "label": "Generated via Impersonation API" + "label": "WebDAV sync" } ] ``` @@ -134,6 +139,9 @@ curl --request POST 'https:///auth-app/tokens?expiry={value}&use --header 'accept: application/json' ``` +The label of a token created this way ends with ` (Impersonation)`, also when a custom `label` +is given, e.g. `WebDAV sync (Impersonation)`. + ### Via CLI (developer only) As the CLI is using the internal CS3Apis this needs access to the reva gateway diff --git a/services/auth-app/pkg/service/service_test.go b/services/auth-app/pkg/service/service_test.go index cbf003b94f..58c0bb0822 100644 --- a/services/auth-app/pkg/service/service_test.go +++ b/services/auth-app/pkg/service/service_test.go @@ -223,3 +223,72 @@ func TestHandleCreateImpersonationExistingUser(t *testing.T) { assert.Equal(t, "app-password", token.Token) assert.Equal(t, "Generated via API (Impersonation)", token.Label) } + +func TestHandleCreateLabel(t *testing.T) { + tests := []struct { + name string + query string + impersonate bool + wantLabel string + }{ + { + name: "no label", + query: "expiry=72h", + wantLabel: "Generated via API", + }, + { + name: "empty label", + query: "expiry=72h&label=", + wantLabel: "Generated via API", + }, + { + name: "custom label", + query: "expiry=72h&label=WebDAV%20sync", + wantLabel: "WebDAV sync", + }, + { + name: "custom label with unicode", + query: "expiry=72h&label=Kalender%20f%C3%BCr%20%F0%9F%93%85", + wantLabel: "Kalender für 📅", + }, + { + name: "impersonation without label", + query: "expiry=72h&userName=alice", + impersonate: true, + wantLabel: "Generated via API (Impersonation)", + }, + { + name: "impersonation keeps its marker on a custom label", + query: "expiry=72h&userName=alice&label=WebDAV%20sync", + impersonate: true, + wantLabel: "WebDAV sync (Impersonation)", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gwc := cs3mocks.NewGatewayAPIClient(t) + if tt.impersonate { + gwc.EXPECT().Authenticate(mock.Anything, mock.Anything).Return(&gateway.AuthenticateResponse{ + Status: &rpc.Status{Code: rpc.Code_CODE_OK}, + User: &userpb.User{Id: &userpb.UserId{OpaqueId: "alice-id"}, Username: "alice"}, + Token: "alice-token", + }, nil).Once() + } + gwc.EXPECT().GenerateAppPassword(mock.Anything, mock.MatchedBy(func(req *applications.GenerateAppPasswordRequest) bool { + return req.GetLabel() == tt.wantLabel + })).Return(&applications.GenerateAppPasswordResponse{ + Status: &rpc.Status{Code: rpc.Code_CODE_OK}, + AppPassword: &applications.AppPassword{Password: "app-password", Label: tt.wantLabel}, + }, nil).Once() + + rec := httptest.NewRecorder() + newTestService(t, gwc, true).ServeHTTP(rec, newCreateRequest(t, tt.query, adminRoleID)) + + require.Equal(t, http.StatusOK, rec.Code) + var token AuthAppToken + require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &token)) + assert.Equal(t, tt.wantLabel, token.Label) + }) + } +} diff --git a/tests/acceptance/bootstrap/AuthAppContext.php b/tests/acceptance/bootstrap/AuthAppContext.php index a5c82e0511..81255e19c8 100644 --- a/tests/acceptance/bootstrap/AuthAppContext.php +++ b/tests/acceptance/bootstrap/AuthAppContext.php @@ -66,6 +66,30 @@ class AuthAppContext implements Context { ); } + /** + * + * @param string $user + * @param string $expiration + * @param string $label + * + * @return void + */ + #[When('user :user creates app token with expiration time :expiration and label :label using the auth-app API')] + public function userCreatesAppTokenWithExpirationTimeAndLabelUsingTheAuthAppApi( + string $user, + string $expiration, + string $label, + ): void { + $this->featureContext->setResponse( + AuthAppHelper::createAppAuthToken( + $this->featureContext->getBaseUrl(), + $this->featureContext->getActualUsername($user), + $this->featureContext->getPasswordForUser($user), + ["expiry" => $expiration, "label" => $label], + ) + ); + } + /** * * @param string $user @@ -154,4 +178,32 @@ class AuthAppContext implements Context { ) ); } + + /** + * + * @param string $impersonatedUser + * @param string $expiration + * @param string $label + * + * @return void + */ + #[When('the administrator creates app token for user :impersonatedUser with expiration time :expiration and label :label using the auth-app API')] + public function theAdministratorCreatesAppTokenForUserWithExpirationTimeAndLabelViaAuthAppApi( + string $impersonatedUser, + string $expiration, + string $label, + ): void { + $this->featureContext->setResponse( + AuthAppHelper::createAppAuthToken( + $this->featureContext->getBaseUrl(), + $this->featureContext->getAdminUsername(), + $this->featureContext->getAdminPassword(), + [ + "expiry" => $expiration, + "userName" => $this->featureContext->getActualUsername($impersonatedUser), + "label" => $label, + ], + ) + ); + } } diff --git a/tests/acceptance/features/apiAuthApp/token.feature b/tests/acceptance/features/apiAuthApp/token.feature index ffcff7e213..f17ebb0c9c 100644 --- a/tests/acceptance/features/apiAuthApp/token.feature +++ b/tests/acceptance/features/apiAuthApp/token.feature @@ -31,6 +31,31 @@ Feature: create auth-app token } """ + + Scenario: user creates auth-app token with a custom label + When user "Alice" creates app token with expiration time "72h" and label "WebDAV sync" using the auth-app API + Then the HTTP status code should be "200" + And the JSON data of the response should match + """ + { + "type": "object", + "required": [ + "token", + "expiration_date", + "created_date", + "label" + ], + "properties": { + "token": { + "pattern": "^([a-z-]+ ){5}[a-z-]+$" + }, + "label": { + "const": "WebDAV sync" + } + } + } + """ + @env-config Scenario: user lists auth-app tokens generated by different auth-app api Given the config "AUTH_APP_ENABLE_IMPERSONATION" has been set to "true" @@ -130,6 +155,32 @@ Feature: create auth-app token } """ + @env-config + Scenario: admin creates auth-app token with a custom label for other user + Given the config "AUTH_APP_ENABLE_IMPERSONATION" has been set to "true" + When the administrator creates app token for user "Alice" with expiration time "72h" and label "WebDAV sync" using the auth-app API + Then the HTTP status code should be "200" + And the JSON data of the response should match + """ + { + "type": "object", + "required": [ + "token", + "expiration_date", + "created_date", + "label" + ], + "properties": { + "token": { + "pattern": "^([a-z-]+ ){5}[a-z-]+$" + }, + "label": { + "const": "WebDAV sync (Impersonation)" + } + } + } + """ + @env-config Scenario: admin tries to create auth-app token for a user that does not exist Given the config "AUTH_APP_ENABLE_IMPERSONATION" has been set to "true"