Skip to content

Commit 80428e0

Browse files
committed
Refactoring to separate vendor consent decoding, encoding and representation concerns. Bumping version to 2.0.0
1 parent 12fc246 commit 80428e0

17 files changed

+1609
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ build/*
55
*.swp
66
gradle-app.setting
77
!gradle-wrapper.jar
8+
### IntelliJ IDEA ###
9+
.idea
10+
*.iws
11+
*.iml
12+
*.ipr
13+
/out/

build.gradle

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@ apply plugin: 'idea'
44

55
repositories {
66
mavenCentral()
7+
mavenLocal()
78
}
89

10+
group = 'com.iab.gdpr'
11+
version = '2.0.0-SNAPSHOT'
12+
13+
description = """A Java implementation of the IAB Consent String spec."""
14+
15+
sourceCompatibility = 1.8
16+
targetCompatibility = 1.8
17+
918
dependencies {
1019
testCompile(
1120
"junit:junit:4.11",

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = 'consent-string-sdk-java'

src/main/java/com/iab/gdpr/VendorConsent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* 20specification%20v1.0a.pdf
2626
*
2727
*/
28+
@Deprecated
2829
public class VendorConsent {
2930
private static Decoder decoder = Base64.getUrlDecoder();
3031
// As per the GDPR framework guidelines padding should be ommitted
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.iab.gdpr.consent;
2+
3+
import java.time.Instant;
4+
import java.util.Set;
5+
6+
/**
7+
* Representation of the values in the vendor consent string.
8+
*
9+
* Combination of {@link VendorConsent#isPurposeAllowed(int)} and {@link VendorConsent#isVendorAllowed(int)} methods
10+
* fully describes user's consent for particular action by a particular vendor
11+
*
12+
*/
13+
public interface VendorConsent {
14+
15+
/**
16+
*
17+
* @return the version of consent string format
18+
*/
19+
int getVersion();
20+
21+
/**
22+
* @return the {@link Instant} at which the consent string was created
23+
*/
24+
Instant getConsentRecordCreated();
25+
26+
/**
27+
*
28+
* @return the {@link Instant} at which consent string was last updated
29+
*/
30+
Instant getConsentRecordLastUpdated();
31+
32+
/**
33+
*
34+
* @return the Consent Manager Provider ID that last updated the consent string
35+
*/
36+
int getCmpId();
37+
38+
/**
39+
*
40+
* @return the Consent Manager Provider version
41+
*/
42+
int getCmpVersion();
43+
44+
/**
45+
*
46+
* @return the screen number in the CMP where consent was given
47+
*/
48+
int getConsentScreen();
49+
50+
/**
51+
*
52+
* @return the two-letter ISO639-1 language code that CMP asked for consent in
53+
*/
54+
String getConsentLanguage();
55+
56+
/**
57+
*
58+
* @return version of vendor list used in most recent consent string update.
59+
*/
60+
int getVendorListVersion();
61+
62+
/**
63+
*
64+
* @return the set of purpose id's which are permitted according to this consent string
65+
*/
66+
Set<Integer> getAllowedPurposes();
67+
68+
/**
69+
*
70+
* @return an integer equivalent of allowed purpose id bits according to this consent string
71+
*/
72+
int getAllowedPurposesBits();
73+
74+
/**
75+
*
76+
* @return the maximum VendorId for which consent values are given.
77+
*/
78+
int getMaxVendorId();
79+
80+
/**
81+
* Check whether purpose with specified ID is allowed
82+
* @param purposeId purpose ID
83+
* @return true if purpose is allowed in this consent, false otherwise
84+
*/
85+
boolean isPurposeAllowed(int purposeId);
86+
87+
/**
88+
* Check whether vendor with specified ID is allowd
89+
* @param vendorId vendor ID
90+
* @return a boolean describing if a user has consented to a particular vendor
91+
*/
92+
boolean isVendorAllowed(int vendorId);
93+
94+
/**
95+
*
96+
* @return the value of this consent as byte array
97+
*/
98+
byte[] toByteArray();
99+
100+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.iab.gdpr.consent;
2+
3+
import com.iab.gdpr.Bits;
4+
import com.iab.gdpr.consent.implementation.v1.ByteBufferBackedVendorConsent;
5+
6+
import java.util.Base64;
7+
8+
import static com.iab.gdpr.GdprConstants.VERSION_BIT_OFFSET;
9+
import static com.iab.gdpr.GdprConstants.VERSION_BIT_SIZE;
10+
11+
/**
12+
* {@link VendorConsent} decoder from Base64 string. Right now only version 1 is know, but eventually
13+
* this can be extended to support new versions
14+
*/
15+
public class VendorConsentDecoder {
16+
17+
private static final Base64.Decoder BASE64_DECODER = Base64.getUrlDecoder();
18+
19+
public static VendorConsent fromBase64String(String consentString) {
20+
if (isNullOrEmpty(consentString))
21+
throw new IllegalArgumentException("Null or empty consent string passed as an argument");
22+
23+
final Bits bits = new Bits(BASE64_DECODER.decode(consentString));
24+
final int version = getVersion(bits);
25+
switch (version) {
26+
case 1:
27+
return new ByteBufferBackedVendorConsent(bits);
28+
default:
29+
throw new IllegalStateException("Unsupported version: " + version);
30+
}
31+
}
32+
33+
/**
34+
* Get the version field from bitmap
35+
* @param bits bitmap
36+
* @return a version number
37+
*/
38+
private static int getVersion(Bits bits) {
39+
return bits.getInt(VERSION_BIT_OFFSET, VERSION_BIT_SIZE);
40+
}
41+
42+
/**
43+
* Utility method to check whether string is empty or null
44+
* @param string value to check
45+
* @return a boolean value of the check
46+
*/
47+
private static boolean isNullOrEmpty(String string) {
48+
return string == null || string.isEmpty();
49+
}
50+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.iab.gdpr.consent;
2+
3+
import java.util.Base64;
4+
5+
/**
6+
* Encode {@link VendorConsent} to Base64 string
7+
*/
8+
public class VendorConsentEncoder {
9+
10+
// As per the GDPR framework guidelines padding should be omitted
11+
private static final Base64.Encoder ENCODER = Base64.getUrlEncoder().withoutPadding();
12+
13+
/**
14+
* Encode vendor consent to Base64 string
15+
* @param vendorConsent vendor consent
16+
* @return Base64 encoded string
17+
*/
18+
public static String toBase64String(VendorConsent vendorConsent) {
19+
return ENCODER.encodeToString(vendorConsent.toByteArray());
20+
}
21+
22+
}

0 commit comments

Comments
 (0)