Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Token service #179

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
id 'io.spring.dependency-management' version "${springDependencyVersion}"
id "jacoco"
id 'project-report'
id 'org.openapi.generator' version '7.1.0'

// used to download the 'dash.jar' for license checks
// docs: https://github.com/michel-kraemer/gradle-download-task
Expand All @@ -14,6 +15,14 @@ group = "${groupName}"
version = "${applicationVersion}"
sourceCompatibility = JavaVersion.VERSION_17

sourceSets {
main {
java {
srcDir "$buildDir/generated/src/main/java"
}
}
}

// alias for Project.getConfigurations()
configurations {
// add a custom config to avoid applying the dev-tools to a production app
Expand Down Expand Up @@ -245,6 +254,22 @@ jacoco {
toolVersion = "${jacocoVersion}"
}

openApiGenerate {
generatorName.set('spring')
inputSpec.set("$rootDir/docs/secure-token-service-openapi.yml")
outputDir.set("$buildDir/generated")

apiPackage.set('org.eclipse.tractusx.managedidentitywallets.adapter.controller')
modelPackage.set('org.eclipse.tractusx.managedidentitywallets.adapter.controller.dto')

configOptions.set([
interfaceOnly: "true",
requestMappingMode: "api_interface",
useSpringBoot3: "true",
skipDefaultInterface: "true",
openApiNullable: "false",
])
}

// 'jacocoTestCoverageVerification' is provided by the 'jacoco' plugin
// docs: https://docs.gradle.org/current/userguide/jacoco_plugin.html#sec:jacoco_report_violation_rules
Expand Down
94 changes: 94 additions & 0 deletions docs/secure-token-service-openapi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
openapi: 3.0.1

Check failure

Code scanning / KICS

Global Security Field Is Undefined (v3) Error documentation

A default security property is not defined

Check warning

Code scanning / KICS

Global Server Object Uses HTTP Warning documentation

Global servers array is not defined

Check notice

Code scanning / KICS

Servers Array Undefined documentation

Servers array does not have at least one server defined

Check warning on line 1 in docs/secure-token-service-openapi.yml

View workflow job for this annotation

GitHub Actions / Analyze

[HIGH] Global Security Field Is Undefined (v3)

Global security field should be defined to prevent API to have insecure paths and have this rules defined on securitySchemes
info:
title: ""
version: ""
contact:
email: "[email protected]"
paths:
/token:
post:

Check failure

Code scanning / KICS

No Global And Operation Security Defined (v3) Error documentation

No security schema is used

Check warning on line 9 in docs/secure-token-service-openapi.yml

View workflow job for this annotation

GitHub Actions / Analyze

[HIGH] No Global And Operation Security Defined (v3)

All paths should have security scheme, if it is omitted, global security field should be defined
operationId: token
requestBody:
content:
application/x-www-form-urlencoded:
encoding:
access_token:
style: form
audience:
style: form
bearer_access_alias:
style: form
bearer_access_scope:
style: form
client_id:
style: form
client_secret:
style: form
grant_type:
style: form
schema:

Check warning

Code scanning / KICS

Additional Properties Too Permissive Warning documentation

'additionalProperties' is not set
type: object
properties:
access_token:
type: string

Check warning

Code scanning / KICS

Maximum Length Undefined (v3) Warning documentation

'maxLength' is undefined

Check warning

Code scanning / KICS

Pattern Undefined (v3) Warning documentation

'pattern' is undefined
description: VP access token to be added as a claim in the SI token
audience:
type: string

Check warning

Code scanning / KICS

Maximum Length Undefined (v3) Warning documentation

'maxLength' is undefined

Check warning

Code scanning / KICS

Pattern Undefined (v3) Warning documentation

'pattern' is undefined
description: Audience for the SI token
bearer_access_alias:
type: string

Check warning

Code scanning / KICS

Maximum Length Undefined (v3) Warning documentation

'maxLength' is undefined

Check warning

Code scanning / KICS

Pattern Undefined (v3) Warning documentation

'pattern' is undefined
description: Alias to be use in the sub of the VP access token (default
is audience)
bearer_access_scope:
type: string

Check warning

Code scanning / KICS

Maximum Length Undefined (v3) Warning documentation

'maxLength' is undefined

Check warning

Code scanning / KICS

Pattern Undefined (v3) Warning documentation

'pattern' is undefined
description: Scope to be added in the VP access token
client_id:
type: string

Check warning

Code scanning / KICS

Maximum Length Undefined (v3) Warning documentation

'maxLength' is undefined

Check warning

Code scanning / KICS

Pattern Undefined (v3) Warning documentation

'pattern' is undefined
description: Id of the client requesting an SI token
client_secret:
type: string

Check warning

Code scanning / KICS

Maximum Length Undefined (v3) Warning documentation

'maxLength' is undefined

Check warning

Code scanning / KICS

Pattern Undefined (v3) Warning documentation

'pattern' is undefined
description: Secret of the client requesting an SI token
grant_type:
type: string

Check warning

Code scanning / KICS

Maximum Length Undefined (v3) Warning documentation

'maxLength' is undefined

Check warning

Code scanning / KICS

Pattern Undefined (v3) Warning documentation

'pattern' is undefined
description: "Type of grant: must be set to client_credentials"
required:
- audience
- client_id
- client_secret
- grant_type
responses:

Check warning

Code scanning / KICS

Default Response Undefined On Operations (v3) Warning documentation

Default field is not defined on responses

Check warning

Code scanning / KICS

Response Code Missing (v3) Warning documentation

500 response is undefined

Check warning

Code scanning / KICS

Response Code Missing (v3) Warning documentation

429 response is undefined

Check warning

Code scanning / KICS

Response Code Missing (v3) Warning documentation

415 response is undefined
"200":
content:
application/json:
schema:

Check warning

Code scanning / KICS

Additional Properties Too Permissive Warning documentation

'additionalProperties' is not set
$ref: '#/components/schemas/StsTokenResponse'

Check warning

Code scanning / KICS

Maximum Length Undefined (v3) Warning documentation

'maxLength' is undefined

Check warning

Code scanning / KICS

Maximum Length Undefined (v3) Warning documentation

'maxLength' is undefined

Check warning

Code scanning / KICS

Numeric Schema Without Maximum (v3) Warning documentation

Numeric schema does not have 'maximum' defined

Check warning

Code scanning / KICS

Numeric Schema Without Minimum (v3) Warning documentation

Numeric schema does not have 'minimum' defined

Check warning

Code scanning / KICS

Pattern Undefined (v3) Warning documentation

'pattern' is undefined

Check warning

Code scanning / KICS

Pattern Undefined (v3) Warning documentation

'pattern' is undefined
description: The Self-Issued ID token
"400":
content:
application/json:
schema:

Check warning

Code scanning / KICS

Additional Properties Too Permissive Warning documentation

'additionalProperties' is not set
type: array

Check failure

Code scanning / KICS

Array Without Maximum Number of Items (v3) Error documentation

Array schema has 'maxItems' set

Check warning on line 70 in docs/secure-token-service-openapi.yml

View workflow job for this annotation

GitHub Actions / Analyze

[HIGH] Array Without Maximum Number of Items (v3)

Array schema should have the field 'maxItems' set
items:
$ref: '#/components/schemas/StsTokenErrorResponse'

Check warning

Code scanning / KICS

Maximum Length Undefined (v3) Warning documentation

'maxLength' is undefined

Check warning

Code scanning / KICS

Maximum Length Undefined (v3) Warning documentation

'maxLength' is undefined

Check warning

Code scanning / KICS

Pattern Undefined (v3) Warning documentation

'pattern' is undefined

Check warning

Code scanning / KICS

Pattern Undefined (v3) Warning documentation

'pattern' is undefined
description: Invalid Request
tags:

Check notice

Code scanning / KICS

Object Without Required Property (v3) documentation

tags is missing required fields
- Secure Token Service Api
components:

Check failure

Code scanning / KICS

Field 'securityScheme' On Components Is Undefined Error documentation

A security scheme is not defined

Check warning on line 76 in docs/secure-token-service-openapi.yml

View workflow job for this annotation

GitHub Actions / Analyze

[HIGH] Field 'securityScheme' On Components Is Undefined

Components' securityScheme field must have a valid scheme
schemas:
StsTokenErrorResponse:

Check warning

Code scanning / KICS

Additional Properties Too Permissive Warning documentation

'additionalProperties' is not set
type: object
properties:
error:
type: string

Check warning

Code scanning / KICS

Maximum Length Undefined (v3) Warning documentation

'maxLength' is undefined

Check warning

Code scanning / KICS

Pattern Undefined (v3) Warning documentation

'pattern' is undefined
error_description:
type: string

Check warning

Code scanning / KICS

Maximum Length Undefined (v3) Warning documentation

'maxLength' is undefined

Check warning

Code scanning / KICS

Pattern Undefined (v3) Warning documentation

'pattern' is undefined
StsTokenResponse:

Check warning

Code scanning / KICS

Additional Properties Too Permissive Warning documentation

'additionalProperties' is not set
type: object
properties:
access_token:
type: string

Check warning

Code scanning / KICS

Maximum Length Undefined (v3) Warning documentation

'maxLength' is undefined

Check warning

Code scanning / KICS

Pattern Undefined (v3) Warning documentation

'pattern' is undefined
expires_in:
type: integer

Check warning

Code scanning / KICS

Numeric Schema Without Maximum (v3) Warning documentation

Numeric schema does not have 'maximum' defined

Check warning

Code scanning / KICS

Numeric Schema Without Minimum (v3) Warning documentation

Numeric schema does not have 'minimum' defined
format: int64
token_type:
type: string

Check warning

Code scanning / KICS

Maximum Length Undefined (v3) Warning documentation

'maxLength' is undefined

Check warning

Code scanning / KICS

Pattern Undefined (v3) Warning documentation

'pattern' is undefined
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* ******************************************************************************
*/

package org.eclipse.tractusx.managedidentitywallets.controller;
package org.eclipse.tractusx.managedidentitywallets.adapter.controller;

import org.eclipse.tractusx.managedidentitywallets.constant.StringPool;
import org.eclipse.tractusx.managedidentitywallets.exception.ForbiddenException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@
* ******************************************************************************
*/

package org.eclipse.tractusx.managedidentitywallets.controller;
package org.eclipse.tractusx.managedidentitywallets.adapter.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.tractusx.managedidentitywallets.apidocs.DidDocumentControllerApiDocs.BpnParameterDoc;
import org.eclipse.tractusx.managedidentitywallets.apidocs.DidDocumentControllerApiDocs.DidOrBpnParameterDoc;
import org.eclipse.tractusx.managedidentitywallets.apidocs.DidDocumentControllerApiDocs.GetDidDocumentApiDocs;
import org.eclipse.tractusx.managedidentitywallets.apidocs.DidDocumentControllerApiDocs.GetDidResolveApiDocs;

import org.eclipse.tractusx.managedidentitywallets.adapter.did.DidDocumentService;
import org.eclipse.tractusx.managedidentitywallets.constant.RestURI;
import org.eclipse.tractusx.managedidentitywallets.service.DidDocumentService;
import org.eclipse.tractusx.ssi.lib.model.did.DidDocument;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* ******************************************************************************
*/

package org.eclipse.tractusx.managedidentitywallets.controller;
package org.eclipse.tractusx.managedidentitywallets.adapter.controller;


import jakarta.validation.constraints.Max;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* ******************************************************************************
*/

package org.eclipse.tractusx.managedidentitywallets.controller;
package org.eclipse.tractusx.managedidentitywallets.adapter.controller;

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ExampleObject;
Expand All @@ -35,10 +35,10 @@
import org.eclipse.tractusx.managedidentitywallets.apidocs.IssuersCredentialControllerApiDocs.IssueFrameworkCredentialApiDocs;
import org.eclipse.tractusx.managedidentitywallets.apidocs.IssuersCredentialControllerApiDocs.IssueVerifiableCredentialUsingBaseWalletApiDocs;
import org.eclipse.tractusx.managedidentitywallets.apidocs.IssuersCredentialControllerApiDocs.ValidateVerifiableCredentialApiDocs;
import org.eclipse.tractusx.managedidentitywallets.adapter.controller.dto.IssueDismantlerCredentialRequest;
import org.eclipse.tractusx.managedidentitywallets.adapter.controller.dto.IssueFrameworkCredentialRequest;
import org.eclipse.tractusx.managedidentitywallets.adapter.controller.dto.IssueMembershipCredentialRequest;
import org.eclipse.tractusx.managedidentitywallets.constant.RestURI;
import org.eclipse.tractusx.managedidentitywallets.dto.IssueDismantlerCredentialRequest;
import org.eclipse.tractusx.managedidentitywallets.dto.IssueFrameworkCredentialRequest;
import org.eclipse.tractusx.managedidentitywallets.dto.IssueMembershipCredentialRequest;
import org.eclipse.tractusx.managedidentitywallets.service.IssuersCredentialService;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
import org.springframework.data.domain.PageImpl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* ******************************************************************************
*/

package org.eclipse.tractusx.managedidentitywallets.controller;
package org.eclipse.tractusx.managedidentitywallets.adapter.controller;

import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.eclipse.tractusx.managedidentitywallets.adapter.controller;

import java.util.Optional;
import java.util.Set;

import org.eclipse.tractusx.managedidentitywallets.adapter.controller.dto.StsTokenErrorResponse;
import org.eclipse.tractusx.managedidentitywallets.adapter.controller.dto.StsTokenResponse;
import org.eclipse.tractusx.managedidentitywallets.adapter.controller.exception.UnsupportedGrantTypeException;
import org.eclipse.tractusx.managedidentitywallets.domain.DID;
import org.eclipse.tractusx.managedidentitywallets.service.SecureTokenService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import com.nimbusds.jwt.JWT;
import com.nimbusds.jwt.JWTParser;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;

@RestController
@RequiredArgsConstructor
public class SecureTokenController implements TokenApi {

private static final String CLIENT_CREDENTIALS = "client_credentials";
private static final String TOKEN_TYPE_BEARER = "Bearer";

private final SecureTokenService tokenService;

@Override
@SneakyThrows
public ResponseEntity<StsTokenResponse> token(
@Valid String audience, @Valid String clientId, @Valid String clientSecret, @Valid String grantType,
@Valid String accessToken, @Valid String bearerAccessAlias, @Valid String bearerAccessScope) {
if (!grantType.equals(CLIENT_CREDENTIALS)) {
throw new UnsupportedGrantTypeException("Selected GrantType is not supported.");
}

// Authentication is handled in {@link
// org.eclipse.tractusx.managedidentitywallets.adapter.controller.filter.ClientCredentialsFilter}
// and {@link
// org.eclipse.tractusx.managedidentitywallets.adapter.controller.filter.FilterConfig}

JWT jwt;
if (accessToken != null && !accessToken.isBlank()) {
jwt = tokenService.issueToken(new DID(clientId), new DID(audience), JWTParser.parse(accessToken));
} else {
jwt = tokenService.issueToken(new DID(clientId), new DID(audience),
Optional.of(bearerAccessScope).map(scopes -> Set.of(scopes.split(" "))).orElse(Set.of()));
}

StsTokenResponse response = new StsTokenResponse();
response.setAccessToken(jwt.toString());
response.setExpiresIn(jwt.getJWTClaimsSet().getExpirationTime().getTime());
response.setTokenType(TOKEN_TYPE_BEARER);
return ResponseEntity.ok().build();
}

@ExceptionHandler(UnsupportedGrantTypeException.class)
public ResponseEntity<StsTokenErrorResponse> getErrorResponse(RuntimeException e) {
StsTokenErrorResponse response = new StsTokenErrorResponse();
response.setError("client_metadata_value_not_supported");
response.setErrorDescription(e.getMessage());
return ResponseEntity.badRequest().body(response);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* ******************************************************************************
*/

package org.eclipse.tractusx.managedidentitywallets.controller;
package org.eclipse.tractusx.managedidentitywallets.adapter.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
Expand All @@ -35,9 +35,9 @@
import org.eclipse.tractusx.managedidentitywallets.apidocs.WalletControllerApiDocs.SortColumnParameterDoc;
import org.eclipse.tractusx.managedidentitywallets.apidocs.WalletControllerApiDocs.SortTypeParameterDoc;
import org.eclipse.tractusx.managedidentitywallets.apidocs.WalletControllerApiDocs.StoreVerifiableCredentialApiDoc;
import org.eclipse.tractusx.managedidentitywallets.adapter.controller.dto.CreateWalletRequest;
import org.eclipse.tractusx.managedidentitywallets.adapter.persistence.dao.entity.Wallet;
import org.eclipse.tractusx.managedidentitywallets.constant.RestURI;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.Wallet;
import org.eclipse.tractusx.managedidentitywallets.dto.CreateWalletRequest;
import org.eclipse.tractusx.managedidentitywallets.service.WalletService;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* ******************************************************************************
*/

package org.eclipse.tractusx.managedidentitywallets.dto;
package org.eclipse.tractusx.managedidentitywallets.adapter.controller.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* ******************************************************************************
*/

package org.eclipse.tractusx.managedidentitywallets.dto;
package org.eclipse.tractusx.managedidentitywallets.adapter.controller.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* ******************************************************************************
*/

package org.eclipse.tractusx.managedidentitywallets.dto;
package org.eclipse.tractusx.managedidentitywallets.adapter.controller.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* ******************************************************************************
*/

package org.eclipse.tractusx.managedidentitywallets.dto;
package org.eclipse.tractusx.managedidentitywallets.adapter.controller.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.eclipse.tractusx.managedidentitywallets.adapter.controller.exception;

public class UnsupportedGrantTypeException extends RuntimeException {

public UnsupportedGrantTypeException(String message) {
super(message);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* ******************************************************************************
*/

package org.eclipse.tractusx.managedidentitywallets.service;
package org.eclipse.tractusx.managedidentitywallets.adapter.did;

import lombok.Getter;
import org.eclipse.tractusx.managedidentitywallets.config.MIWSettings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
* ******************************************************************************
*/

package org.eclipse.tractusx.managedidentitywallets.service;
package org.eclipse.tractusx.managedidentitywallets.adapter.did;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.eclipse.tractusx.managedidentitywallets.service.CommonService;
import org.eclipse.tractusx.ssi.lib.model.did.DidDocument;
import org.springframework.stereotype.Service;

Expand Down
Loading
Loading