Skip to content

Commit

Permalink
Fix api key details
Browse files Browse the repository at this point in the history
  • Loading branch information
Portals committed Jun 9, 2024
1 parent 1ba03b1 commit 2ebc365
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,30 @@ public ModelAndView getCreateApiKey(
public ModelAndView createApiKey(
@RequestHeader(value = "HX-Request", required = false) boolean htmxRequest,
CreateApiKey form,
BindingResult bindingResult) {
BindingResult bindingResult,
HttpServletResponse response) {
ModelAndView mv = new ModelAndView();

ApiKeyFacade.CreatedApiKey apiKeyCredentials =
ApiKeyFacade.CreatedApiKey createdApiKey =
this.apiKeyFacade.create(
new ApiKeyFacade.NewApiKey(
form.prettyName, form.svDescription, form.enDescription, form.keyType));

mv.setViewName("pages/api-key-credentials");
mv.addObject("apiKeyId", apiKeyCredentials.apiKeyId());
mv.addObject("apiKeyToken", apiKeyCredentials.token());
mv.addObject("name", form.prettyName);
UUID apiKeyId = createdApiKey.apiKey().id();

mv.setViewName("pages/api-key-details");
mv.addObject("apiKey", createdApiKey.apiKey());
mv.addObject("apiKeyId", apiKeyId);
mv.addObject("apiKeyToken", createdApiKey.token());

String type = createdApiKey.apiKey().keyType();
if (type.equals("ACCOUNT_SCAFFOLD")) {
loadApiKeySettingsAccountScaffold(mv, apiKeyId);
} else if (type.equals("INFO")) {
loadApiKeySettingsInfo(mv, apiKeyId);
}

response.addHeader("HX-Push-Url", "/api-keys/" + apiKeyId);

return mv;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public ModelAndView createClient(
mv.addObject("clientSecret", result.clientSecret());
mv.addObject("apiKeyToken", result.apiKeyToken());

response.addHeader("HX-Push-Url", "/clients/" + result.client().clientUid().toString());
response.addHeader("HX-Push-Url", "/clients/" + result.client().clientUid());

return mv;
}
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/it/chalmers/gamma/app/apikey/ApiKeyFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public String[] getApiKeyTypes() {
return s;
}

public record CreatedApiKey(UUID apiKeyId, String token) {}
public record CreatedApiKey(ApiKeyDTO apiKey, String token) {}

@Transactional
public CreatedApiKey create(NewApiKey newApiKey) {
Expand All @@ -61,21 +61,23 @@ public CreatedApiKey create(NewApiKey newApiKey) {

ApiKeyId apiKeyId = ApiKeyId.generate();
ApiKeyToken.GeneratedApiKeyToken generated = ApiKeyToken.generate(passwordEncoder);
apiKeyRepository.create(
ApiKey apiKey =
new ApiKey(
apiKeyId,
new PrettyName(newApiKey.prettyName),
new Text(newApiKey.svDescription, newApiKey.enDescription),
type,
generated.apiKeyToken()));
generated.apiKeyToken());

apiKeyRepository.create(apiKey);

if (type == ApiKeyType.INFO) {
this.apiKeySettingsRepository.createEmptyInfoSettings(apiKeyId);
} else if (type == ApiKeyType.ACCOUNT_SCAFFOLD) {
this.apiKeySettingsRepository.createEmptyAccountScaffoldSettings(apiKeyId);
}

return new CreatedApiKey(apiKeyId.value(), generated.rawToken());
return new CreatedApiKey(new ApiKeyDTO(apiKey), generated.rawToken());
}

public void delete(UUID apiKeyId) throws ApiKeyNotFoundException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void ensureApiKeys() {
for (ApiKeyType apiKeyType : ApiKeyType.values()) {
if (apiKeyType != ApiKeyType.CLIENT) {

var secrets =
var createdApiKey =
this.apiKeyFacade.create(
new ApiKeyFacade.NewApiKey(
apiKeyType.name().toLowerCase() + "-mock", "", "", apiKeyType.name()));
Expand All @@ -39,9 +39,9 @@ public void ensureApiKeys() {
"Api key of type "
+ apiKeyType.name()
+ " has been generated with id: "
+ secrets.apiKeyId()
+ createdApiKey.apiKey().id()
+ " and code: "
+ secrets.token());
+ createdApiKey.token());
}
}

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/resources/templates/pages/api-key-details.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<div id="alerts">
</div>
<main>
<th:block th:if="${apiKeyToken!= null}">
<article th:replace="~{partial/api-key-credentials}"></article>
</th:block>

<article th:fragment="details">
<header>
Api key details
Expand Down
29 changes: 19 additions & 10 deletions app/src/main/resources/templates/partial/api-key-credentials.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
<article>
<header th:text="|Credentials for ${name}|">
<header>
Credentials
</header>
<p>These are the credentials for your api key. They will not be shown again.</p>
<ul class="tuple">
<li>
<span>Api key</span>
<span th:text="${apiKeyToken}"></span>
</li>
</ul>
<footer>
<a th:href="@{/api-keys/{id}(id=${apiKeyId})}">Go to api key</a>
</footer>
<div class="tuple-right-expand">
<div th:if="${apiKeyToken != null}">
<span>Api key:</span>
<code th:text="${apiKeyToken}"></code>
</div>
</div>
<th:block th:if="${apiKeyToken != null}">
<p>
Read more here about how to use Client API here: <br><a href="https://github.com/cthit/Gamma/wiki/Client-API">github.com/cthit/Gamma/wiki/Client-API</a>
</p>
<div>
To authorize when doing API requests, simply add this header:
</div>
<p>
<code>Authorization: pre-shared <span th:text="${apiKeyId}"></span>:<span th:text="${apiKeyToken}"></span></code>
</p>
</th:block>
</article>

0 comments on commit 2ebc365

Please sign in to comment.