Skip to content

Commit 1a02048

Browse files
authored
JCL-366: Address sonar warnings for access grant module (#487)
* JCL-366: Address sonar warnings for access grant module * Remove use of deprecated method
1 parent 05391ff commit 1a02048

7 files changed

Lines changed: 63 additions & 51 deletions

File tree

‎access-grant/src/main/java/com/inrupt/client/accessgrant/AccessCredential.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ static CredentialMetadata extractMetadata(final Map data) {
315315
statusTypes.contains(REVOCATION_LIST_2020_STATUS)).map(x ->
316316
asRevocationList2020(credentialStatus))).orElse(null);
317317

318-
final Map subject = asMap(data.get("credentialSubject")).orElseThrow(() ->
318+
final Map<String, Object> subject = asMap(data.get("credentialSubject")).orElseThrow(() ->
319319
new IllegalArgumentException("Missing or invalid credentialSubject field"));
320320

321321
final URI creator = asUri(subject.get("id")).orElseThrow(() ->
@@ -324,8 +324,8 @@ static CredentialMetadata extractMetadata(final Map data) {
324324
return new CredentialMetadata(issuer, creator, types, expiration, status);
325325
}
326326

327-
static Map extractConsent(final Map data, final String property) {
328-
final Map subject = asMap(data.get("credentialSubject")).orElseThrow(() ->
327+
static Map<String, Object> extractConsent(final Map data, final String property) {
328+
final Map<String, Object> subject = asMap(data.get("credentialSubject")).orElseThrow(() ->
329329
new IllegalArgumentException("Missing or invalid credentialSubject field"));
330330
return asMap(subject.get(property)).orElseThrow(() ->
331331
// Unsupported structure

‎access-grant/src/main/java/com/inrupt/client/accessgrant/AccessDenial.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ static AccessDenial parse(final String serialization) throws IOException {
103103
final Map<String, Object> data = jsonService.fromJson(in,
104104
new HashMap<String, Object>(){}.getClass().getGenericSuperclass());
105105

106-
final List<Map> vcs = getCredentialsFromPresentation(data, supportedTypes);
106+
final List<Map<String, Object>> vcs = getCredentialsFromPresentation(data, supportedTypes);
107107
if (vcs.size() != 1) {
108108
throw new IllegalArgumentException(
109109
"Invalid Access Denial: ambiguous number of verifiable credentials");
110110
}
111-
final Map vc = vcs.get(0);
111+
final Map<String, Object> vc = vcs.get(0);
112112

113113
if (asSet(data.get(TYPE)).orElseGet(Collections::emptySet).contains("VerifiablePresentation")) {
114114
final URI identifier = asUri(vc.get("id")).orElseThrow(() ->
@@ -119,7 +119,7 @@ static AccessDenial parse(final String serialization) throws IOException {
119119

120120

121121
// Extract V1 Access Denial data, using gConsent
122-
final Map consent = extractConsent(vc, "providedConsent");
122+
final Map<String, Object> consent = extractConsent(vc, "providedConsent");
123123

124124
final Optional<URI> person = asUri(consent.get("isProvidedToPerson"));
125125
final Optional<URI> controller = asUri(consent.get("isProvidedToController"));

‎access-grant/src/main/java/com/inrupt/client/accessgrant/AccessGrant.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,12 @@ static AccessGrant parse(final String serialization) throws IOException {
171171
final Map<String, Object> data = jsonService.fromJson(in,
172172
new HashMap<String, Object>(){}.getClass().getGenericSuperclass());
173173

174-
final List<Map> vcs = getCredentialsFromPresentation(data, supportedTypes);
174+
final List<Map<String, Object>> vcs = getCredentialsFromPresentation(data, supportedTypes);
175175
if (vcs.size() != 1) {
176176
throw new IllegalArgumentException(
177177
"Invalid Access Grant: ambiguous number of verifiable credentials");
178178
}
179-
final Map vc = vcs.get(0);
179+
final Map<String, Object> vc = vcs.get(0);
180180

181181
if (asSet(data.get(TYPE)).orElseGet(Collections::emptySet).contains("VerifiablePresentation")) {
182182
final URI identifier = asUri(vc.get("id")).orElseThrow(() ->
@@ -186,7 +186,7 @@ static AccessGrant parse(final String serialization) throws IOException {
186186
final CredentialMetadata credentialMetadata = extractMetadata(vc);
187187

188188
// Extract V1 Access Grant data, using gConsent
189-
final Map consent = extractConsent(vc, "providedConsent");
189+
final Map<String, Object> consent = extractConsent(vc, "providedConsent");
190190

191191
final Optional<URI> person = asUri(consent.get("isProvidedToPerson"));
192192
final Optional<URI> controller = asUri(consent.get("isProvidedToController"));

‎access-grant/src/main/java/com/inrupt/client/accessgrant/AccessGrantClient.java‎

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ public class AccessGrantClient {
9797
private static final String FOR_PERSONAL_DATA = "forPersonalData";
9898
private static final String HAS_STATUS = "hasStatus";
9999
private static final String MODE = "mode";
100+
private static final String PROVIDED_CONSENT = "providedConsent";
101+
private static final String FOR_PURPOSE = "forPurpose";
102+
private static final String EXPIRATION_DATE = "expirationDate";
103+
private static final String CREDENTIAL = "credential";
100104
private static final URI ACCESS_GRANT = URI.create("http://www.w3.org/ns/solid/vc#SolidAccessGrant");
101105
private static final URI ACCESS_REQUEST = URI.create("http://www.w3.org/ns/solid/vc#SolidAccessRequest");
102106
private static final URI ACCESS_DENIAL = URI.create("http://www.w3.org/ns/solid/vc#SolidAccessDenial");
@@ -592,26 +596,24 @@ <T extends AccessCredential> List<T> processQueryResponse(final InputStream inpu
592596
final Map<String, Object> data = jsonService.fromJson(input,
593597
new HashMap<String, Object>(){}.getClass().getGenericSuperclass());
594598
final List<T> grants = new ArrayList<>();
595-
if (data.get(VERIFIABLE_CREDENTIAL) instanceof Collection) {
596-
for (final Object item : (Collection) data.get(VERIFIABLE_CREDENTIAL)) {
597-
Utils.asMap(item).ifPresent(credential ->
598-
Utils.asSet(credential.get(TYPE)).ifPresent(types -> {
599-
types.retainAll(validTypes);
600-
if (!types.isEmpty()) {
601-
final Map<String, Object> presentation = new HashMap<>();
602-
presentation.put(CONTEXT, Arrays.asList(VC_CONTEXT_URI));
603-
presentation.put(TYPE, Arrays.asList("VerifiablePresentation"));
604-
presentation.put(VERIFIABLE_CREDENTIAL, Arrays.asList(credential));
605-
if (AccessGrant.class.equals(clazz)) {
606-
grants.add((T) AccessGrant.of(new String(serialize(presentation), UTF_8)));
607-
} else if (AccessRequest.class.equals(clazz)) {
608-
grants.add((T) AccessRequest.of(new String(serialize(presentation), UTF_8)));
609-
} else if (AccessDenial.class.equals(clazz)) {
610-
grants.add((T) AccessDenial.of(new String(serialize(presentation), UTF_8)));
611-
}
599+
for (final Object item : getCredentials(data)) {
600+
Utils.asMap(item).ifPresent(credential ->
601+
Utils.asSet(credential.get(TYPE)).ifPresent(types -> {
602+
types.retainAll(validTypes);
603+
if (!types.isEmpty()) {
604+
final Map<String, Object> presentation = new HashMap<>();
605+
presentation.put(CONTEXT, Arrays.asList(VC_CONTEXT_URI));
606+
presentation.put(TYPE, Arrays.asList("VerifiablePresentation"));
607+
presentation.put(VERIFIABLE_CREDENTIAL, Arrays.asList(credential));
608+
if (AccessGrant.class.equals(clazz)) {
609+
grants.add((T) AccessGrant.of(new String(serialize(presentation), UTF_8)));
610+
} else if (AccessRequest.class.equals(clazz)) {
611+
grants.add((T) AccessRequest.of(new String(serialize(presentation), UTF_8)));
612+
} else if (AccessDenial.class.equals(clazz)) {
613+
grants.add((T) AccessDenial.of(new String(serialize(presentation), UTF_8)));
612614
}
613-
}));
614-
}
615+
}
616+
}));
615617
}
616618

617619
return grants;
@@ -661,6 +663,14 @@ byte[] serialize(final Map<String, Object> data) {
661663
}
662664
}
663665

666+
static Collection<Object> getCredentials(final Map<String, Object> data) {
667+
final Object credential = data.get(VERIFIABLE_CREDENTIAL);
668+
if (credential instanceof Collection) {
669+
return (Collection) credential;
670+
}
671+
return Collections.emptyList();
672+
}
673+
664674
static List<Map<String, Object>> buildQuery(final URI issuer, final URI type, final URI agent, final URI resource,
665675
final String mode) {
666676
final List<Map<String, Object>> queries = new ArrayList<>();
@@ -693,7 +703,7 @@ static void buildQuery(final List<Map<String, Object>> queries, final URI issuer
693703
final Map<String, Object> subject = new HashMap<>();
694704
if (!consent.isEmpty()) {
695705
if (isAccessGrant(type) || isAccessDenial(type)) {
696-
subject.put("providedConsent", consent);
706+
subject.put(PROVIDED_CONSENT, consent);
697707
} else if (isAccessRequest(type)) {
698708
subject.put("hasConsent", consent);
699709
}
@@ -745,21 +755,21 @@ static Map<String, Object> buildAccessDenialv1(final URI agent, final Set<URI> r
745755
consent.put(FOR_PERSONAL_DATA, resources);
746756
consent.put(IS_PROVIDED_TO_PERSON, agent);
747757
if (!purposes.isEmpty()) {
748-
consent.put("forPurpose", purposes);
758+
consent.put(FOR_PURPOSE, purposes);
749759
}
750760

751761
final Map<String, Object> subject = new HashMap<>();
752-
subject.put("providedConsent", consent);
762+
subject.put(PROVIDED_CONSENT, consent);
753763

754764
final Map<String, Object> credential = new HashMap<>();
755765
credential.put(CONTEXT, Arrays.asList(VC_CONTEXT_URI, INRUPT_CONTEXT_URI));
756766
if (expiration != null) {
757-
credential.put("expirationDate", expiration.truncatedTo(ChronoUnit.SECONDS).toString());
767+
credential.put(EXPIRATION_DATE, expiration.truncatedTo(ChronoUnit.SECONDS).toString());
758768
}
759769
credential.put(CREDENTIAL_SUBJECT, subject);
760770

761771
final Map<String, Object> data = new HashMap<>();
762-
data.put("credential", credential);
772+
data.put(CREDENTIAL, credential);
763773
return data;
764774
}
765775

@@ -772,21 +782,21 @@ static Map<String, Object> buildAccessGrantv1(final URI agent, final Set<URI> re
772782
consent.put(FOR_PERSONAL_DATA, resources);
773783
consent.put(IS_PROVIDED_TO_PERSON, agent);
774784
if (!purposes.isEmpty()) {
775-
consent.put("forPurpose", purposes);
785+
consent.put(FOR_PURPOSE, purposes);
776786
}
777787

778788
final Map<String, Object> subject = new HashMap<>();
779-
subject.put("providedConsent", consent);
789+
subject.put(PROVIDED_CONSENT, consent);
780790

781791
final Map<String, Object> credential = new HashMap<>();
782792
credential.put(CONTEXT, Arrays.asList(VC_CONTEXT_URI, INRUPT_CONTEXT_URI));
783793
if (expiration != null) {
784-
credential.put("expirationDate", expiration.truncatedTo(ChronoUnit.SECONDS).toString());
794+
credential.put(EXPIRATION_DATE, expiration.truncatedTo(ChronoUnit.SECONDS).toString());
785795
}
786796
credential.put(CREDENTIAL_SUBJECT, subject);
787797

788798
final Map<String, Object> data = new HashMap<>();
789-
data.put("credential", credential);
799+
data.put(CREDENTIAL, credential);
790800
return data;
791801
}
792802

@@ -800,7 +810,7 @@ static Map<String, Object> buildAccessRequestv1(final URI agent, final Set<URI>
800810
consent.put(IS_CONSENT_FOR_DATA_SUBJECT, agent);
801811
}
802812
if (!purposes.isEmpty()) {
803-
consent.put("forPurpose", purposes);
813+
consent.put(FOR_PURPOSE, purposes);
804814
}
805815

806816
final Map<String, Object> subject = new HashMap<>();
@@ -809,12 +819,12 @@ static Map<String, Object> buildAccessRequestv1(final URI agent, final Set<URI>
809819
final Map<String, Object> credential = new HashMap<>();
810820
credential.put(CONTEXT, Arrays.asList(VC_CONTEXT_URI, INRUPT_CONTEXT_URI));
811821
if (expiration != null) {
812-
credential.put("expirationDate", expiration.truncatedTo(ChronoUnit.SECONDS).toString());
822+
credential.put(EXPIRATION_DATE, expiration.truncatedTo(ChronoUnit.SECONDS).toString());
813823
}
814824
credential.put(CREDENTIAL_SUBJECT, subject);
815825

816826
final Map<String, Object> data = new HashMap<>();
817-
data.put("credential", credential);
827+
data.put(CREDENTIAL, credential);
818828
return data;
819829
}
820830

‎access-grant/src/main/java/com/inrupt/client/accessgrant/AccessGrantSession.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public Optional<Credential> getCredential(final URI name, final URI uri) {
128128
if (isAncestor(entry.getKey(), uri)) {
129129
final AccessGrant grant = entry.getValue();
130130
return Optional.of(new Credential("", grant.getIssuer(), Base64.getUrlEncoder().withoutPadding()
131-
.encodeToString(grant.getRawGrant().getBytes(UTF_8)),
131+
.encodeToString(grant.serialize().getBytes(UTF_8)),
132132
grant.getExpiration(), session.getPrincipal().orElse(null), null));
133133
}
134134
}

‎access-grant/src/main/java/com/inrupt/client/accessgrant/AccessRequest.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ static AccessRequest parse(final String serialization) throws IOException {
102102
final Map<String, Object> data = jsonService.fromJson(in,
103103
new HashMap<String, Object>(){}.getClass().getGenericSuperclass());
104104

105-
final List<Map> vcs = getCredentialsFromPresentation(data, supportedTypes);
105+
final List<Map<String, Object>> vcs = getCredentialsFromPresentation(data, supportedTypes);
106106
if (vcs.size() != 1) {
107107
throw new IllegalArgumentException(
108108
"Invalid Access Request: ambiguous number of verifiable credentials");
109109
}
110-
final Map vc = vcs.get(0);
110+
final Map<String, Object> vc = vcs.get(0);
111111

112112
if (asSet(data.get(TYPE)).orElseGet(Collections::emptySet).contains("VerifiablePresentation")) {
113113
final URI identifier = asUri(vc.get("id")).orElseThrow(() ->
@@ -117,7 +117,7 @@ static AccessRequest parse(final String serialization) throws IOException {
117117
final CredentialMetadata credentialMetadata = extractMetadata(vc);
118118

119119
// V1 Access Request, using gConsent
120-
final Map consent = extractConsent(vc, "hasConsent");
120+
final Map<String, Object> consent = extractConsent(vc, "hasConsent");
121121

122122
final URI recipient = asUri(consent.get("isConsentForDataSubject")).orElse(null);
123123
final Set<String> modes = asSet(consent.get("mode")).orElseGet(Collections::emptySet);

‎access-grant/src/main/java/com/inrupt/client/accessgrant/Utils.java‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ final class Utils {
3636
private static final String TYPE = "type";
3737
private static final String REVOCATION_LIST_2020_STATUS = "RevocationList2020Status";
3838

39-
public static Status asRevocationList2020(final Map credentialStatus) {
39+
public static Status asRevocationList2020(final Map<String, Object> credentialStatus) {
4040
try {
4141
int idx = -1;
4242
final Object index = credentialStatus.get("revocationListIndex");
@@ -72,9 +72,11 @@ public static Optional<URI> asUri(final Object value) {
7272
return Optional.empty();
7373
}
7474

75-
public static Optional<Map> asMap(final Object value) {
75+
public static Optional<Map<String, Object>> asMap(final Object value) {
7676
if (value instanceof Map) {
77-
return Optional.of((Map) value);
77+
@SuppressWarnings("unchecked")
78+
final Map<String, Object> v = (Map<String, Object>) value;
79+
return Optional.of(v);
7880
}
7981
return Optional.empty();
8082
}
@@ -96,13 +98,14 @@ public static Optional<Set<String>> asSet(final Object value) {
9698
return Optional.empty();
9799
}
98100

99-
public static List<Map> getCredentialsFromPresentation(final Map<String, Object> data,
101+
public static List<Map<String, Object>> getCredentialsFromPresentation(final Map<String, Object> data,
100102
final Set<String> supportedTypes) {
101-
final List<Map> credentials = new ArrayList<>();
103+
final List<Map<String, Object>> credentials = new ArrayList<>();
102104
if (data.get("verifiableCredential") instanceof Collection) {
103105
for (final Object item : (Collection) data.get("verifiableCredential")) {
104106
if (item instanceof Map) {
105-
final Map vc = (Map) item;
107+
@SuppressWarnings("unchecked")
108+
final Map vc = (Map<String, Object>) item;
106109
if (asSet(vc.get(TYPE)).filter(types ->
107110
types.stream().anyMatch(supportedTypes::contains)).isPresent()) {
108111
credentials.add(vc);
@@ -113,7 +116,6 @@ public static List<Map> getCredentialsFromPresentation(final Map<String, Object>
113116
return credentials;
114117
}
115118

116-
117119
private Utils() {
118120
// prevent instantiation
119121
}

0 commit comments

Comments
 (0)