Skip to content

Commit 7583dc6

Browse files
authored
merging changes to token cache (#41)
* merging changes to token cache
1 parent bcf5d5d commit 7583dc6

File tree

7 files changed

+129
-63
lines changed

7 files changed

+129
-63
lines changed

src/main/java/com/microsoft/aad/msal4j/AccessTokenCacheEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
class AccessTokenCacheEntity extends Credential {
3838

3939
@SerializedName("credential_type")
40-
private String credentialType = "AccessToken";
40+
private String credentialType;
4141

4242
@SerializedName("realm")
4343
protected String realm;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// All rights reserved.
3+
//
4+
// This code is licensed under the MIT License.
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files(the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions :
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
// THE SOFTWARE.
23+
24+
package com.microsoft.aad.msal4j;
25+
26+
27+
import lombok.AllArgsConstructor;
28+
import lombok.Getter;
29+
import lombok.experimental.Accessors;
30+
31+
@Accessors(fluent = true)
32+
@Getter
33+
@AllArgsConstructor
34+
public enum CredentialTypeEnum {
35+
36+
ACCESS_TOKEN("AccessToken"),
37+
REFRESH_TOKEN("RefreshToken"),
38+
ID_TOKEN("IdToken");
39+
40+
private final String value;
41+
}

src/main/java/com/microsoft/aad/msal4j/IdTokenCacheEntity.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,20 @@
2626
import com.google.gson.annotations.SerializedName;
2727
import com.microsoft.aad.msal4j.Constants;
2828
import com.microsoft.aad.msal4j.Credential;
29+
import lombok.Getter;
30+
import lombok.Setter;
31+
import lombok.experimental.Accessors;
2932

3033
import java.util.ArrayList;
3134
import java.util.List;
3235

36+
@Accessors(fluent = true)
37+
@Getter
38+
@Setter
3339
class IdTokenCacheEntity extends Credential {
3440

3541
@SerializedName("credential_type")
36-
private String credentialType = "IdToken";
42+
private String credentialType;
3743

3844
@SerializedName("realm")
3945
protected String realm;
@@ -52,12 +58,4 @@ String getKey(){
5258

5359
return String.join(Constants.CACHE_KEY_SEPARATOR, keyParts).toLowerCase();
5460
}
55-
56-
String getRealm() {
57-
return realm;
58-
}
59-
60-
void setRealm(String realm) {
61-
this.realm = realm;
62-
}
6361
}

src/main/java/com/microsoft/aad/msal4j/RefreshTokenCacheEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
class RefreshTokenCacheEntity extends Credential {
3838

3939
@SerializedName("credential_type")
40-
private String credentialType = "RefreshToken";
40+
private String credentialType;
4141

4242
@SerializedName("family_id")
4343
private String family_id;

src/main/java/com/microsoft/aad/msal4j/TokenCache.java

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323

2424
package com.microsoft.aad.msal4j;
2525

26-
import com.google.gson.Gson;
27-
import com.google.gson.GsonBuilder;
26+
import com.google.gson.*;
2827
import com.google.gson.annotations.SerializedName;
2928
import com.google.gson.internal.LinkedTreeMap;
3029

@@ -80,23 +79,40 @@ public void deserialize(String data) {
8079
this.appMetadata = deserializedCache.appMetadata;
8180
}
8281

82+
private static void mergeJsonObjects(JsonObject old, JsonObject update) {
83+
84+
for (Map.Entry<String, JsonElement> uEntry : update.entrySet())
85+
{
86+
String key = uEntry.getKey();
87+
JsonElement uValue = uEntry.getValue();
88+
if (!old.has(key)) {
89+
if(!uValue.isJsonNull() &&
90+
!(uValue.isJsonObject() && uValue.getAsJsonObject().size() == 0)){
91+
old.add(key, uValue);
92+
}
93+
}
94+
else{
95+
JsonElement oValue = old.get(key);
96+
if(uValue.isJsonObject()){
97+
mergeJsonObjects(oValue.getAsJsonObject(), uValue.getAsJsonObject());
98+
}
99+
else{
100+
old.add(key, uValue);
101+
}
102+
}
103+
}
104+
}
105+
83106
@Override
84107
public String serialize() {
85108
if(!StringHelper.isBlank(serializedCachedData)){
86-
Object o = new Gson().fromJson(serializedCachedData, Object.class);
87-
Map<String, Object> map = (Map<String, Object>)o;
88-
89-
map.put("AccessToken", accessTokens);
90-
map.put("RefreshToken", refreshTokens);
109+
JsonObject cache = new JsonParser().parse(serializedCachedData).getAsJsonObject();
110+
JsonObject update = new Gson().toJsonTree(this).getAsJsonObject();
91111

92-
map.put("IdToken", idTokens);
93-
map.put("AccountCacheEntity", accounts);
112+
mergeJsonObjects(cache, update);
94113

95-
map.put("AppMetadata", appMetadata);
96-
97-
return new GsonBuilder().create().toJson(map);
114+
return cache.toString();
98115
}
99-
100116
return new GsonBuilder().create().toJson(this);
101117
}
102118

@@ -154,6 +170,7 @@ static RefreshTokenCacheEntity createRefreshTokenCacheEntity(TokenRequest tokenR
154170
AuthenticationResult authenticationResult,
155171
String environmentAlias) {
156172
RefreshTokenCacheEntity rt = new RefreshTokenCacheEntity();
173+
rt.credentialType(CredentialTypeEnum.REFRESH_TOKEN.value());
157174

158175
if(authenticationResult.account() != null){
159176
rt.homeAccountId(authenticationResult.account().homeAccountId());
@@ -172,6 +189,7 @@ static AccessTokenCacheEntity createAccessTokenCacheEntity(TokenRequest tokenReq
172189
AuthenticationResult authenticationResult,
173190
String environmentAlias) {
174191
AccessTokenCacheEntity at = new AccessTokenCacheEntity();
192+
at.credentialType(CredentialTypeEnum.ACCESS_TOKEN.value());
175193

176194
if(authenticationResult.account() != null){
177195
at.homeAccountId(authenticationResult.account().homeAccountId());
@@ -204,6 +222,7 @@ static IdTokenCacheEntity createIdTokenCacheEntity(TokenRequest tokenRequest,
204222
AuthenticationResult authenticationResult,
205223
String environmentAlias) {
206224
IdTokenCacheEntity idToken = new IdTokenCacheEntity();
225+
idToken.credentialType(CredentialTypeEnum.ID_TOKEN.value());
207226

208227
if(authenticationResult.account() != null){
209228
idToken.homeAccountId(authenticationResult.account().homeAccountId());
@@ -214,7 +233,7 @@ static IdTokenCacheEntity createIdTokenCacheEntity(TokenRequest tokenRequest,
214233

215234
IdToken idTokenObj = authenticationResult.idTokenObject();
216235
if (idTokenObj != null) {
217-
idToken.setRealm(idTokenObj.tenantIdentifier);
236+
idToken.realm(idTokenObj.tenantIdentifier);
218237
}
219238

220239
return idToken;

src/main/java/com/microsoft/aad/msal4j/TokenResponse.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ static TokenResponse parseJsonObject(final JSONObject jsonObject)
119119
}
120120

121121
String foci = null;
122-
if (jsonObject.containsKey("familyId")) {
123-
foci = JSONObjectUtils.getString(jsonObject, "familyId");
122+
if (jsonObject.containsKey("foci")) {
123+
foci = JSONObjectUtils.getString(jsonObject, "foci");
124124
}
125125

126126
return new TokenResponse(accessToken, refreshToken,
Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,60 @@
11
{
2-
"AccessToken": {
3-
"9f4880d8-80ba-4c40-97bc-f7a23c703084.f645ad92-e38d-4d1a-b510-d1b09a74a8ca-login.microsoftonline.com-accesstoken-b6c69a37-df96-4db0-9088-2ab96e1d8215-f645ad92-e38d-4d1a-b510-d1b09a74a8ca-calendars.read openid profile tasks.read user.read email": {
4-
"credential_type": "AccessToken",
5-
"realm": "f645ad92-e38d-4d1a-b510-d1b09a74a8ca",
6-
"target": "Calendars.Read openid profile Tasks.Read User.Read email",
7-
"cached_at": "1553397772",
8-
"expires_on": "1553401371",
9-
"extended_expires_on": "1553660571",
10-
"home_account_id": "9f4880d8-80ba-4c40-97bc-f7a23c703084.f645ad92-e38d-4d1a-b510-d1b09a74a8ca",
11-
"environment": "login.microsoftonline.com",
12-
"client_id": "b6c69a37-df96-4db0-9088-2ab96e1d8215",
13-
"secret": "\u003cremoved_at\u003e"
2+
"Account": {
3+
"uid.utid-login.example.com-contoso": {
4+
"username": "John Doe",
5+
"local_account_id": "object1234",
6+
"realm": "contoso",
7+
"environment": "login.example.com",
8+
"home_account_id": "uid.utid",
9+
"authority_type": "MSSTS"
1410
}
1511
},
1612
"RefreshToken": {
17-
"9f4880d8-80ba-4c40-97bc-f7a23c703084.f645ad92-e38d-4d1a-b510-d1b09a74a8ca-login.microsoftonline.com-refreshtoken-b6c69a37-df96-4db0-9088-2ab96e1d8215--": {
13+
"uid.utid-login.example.com-refreshtoken-my_client_id--s2 s1 s3": {
14+
"target": "s2 s1 s3",
15+
"environment": "login.example.com",
1816
"credential_type": "RefreshToken",
19-
"home_account_id": "9f4880d8-80ba-4c40-97bc-f7a23c703084.f645ad92-e38d-4d1a-b510-d1b09a74a8ca",
20-
"environment": "login.microsoftonline.com",
21-
"client_id": "b6c69a37-df96-4db0-9088-2ab96e1d8215",
22-
"secret": "\u003cremoved_rt\u003e"
17+
"secret": "a refresh token",
18+
"client_id": "my_client_id",
19+
"home_account_id": "uid.utid"
2320
}
2421
},
25-
"IdToken": {
26-
"9f4880d8-80ba-4c40-97bc-f7a23c703084.f645ad92-e38d-4d1a-b510-d1b09a74a8ca-login.microsoftonline.com-idtoken-b6c69a37-df96-4db0-9088-2ab96e1d8215-f645ad92-e38d-4d1a-b510-d1b09a74a8ca-": {
27-
"credential_type": "IdToken",
28-
"realm": "f645ad92-e38d-4d1a-b510-d1b09a74a8ca",
29-
"home_account_id": "9f4880d8-80ba-4c40-97bc-f7a23c703084.f645ad92-e38d-4d1a-b510-d1b09a74a8ca",
30-
"environment": "login.microsoftonline.com",
31-
"client_id": "b6c69a37-df96-4db0-9088-2ab96e1d8215",
32-
"secret": "eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.ew0KICAiYXVkIjogImI2YzY5YTM3LWRmOTYtNGRiMC05MDg4LTJhYjk2ZTFkODIxNSIsDQogICJpc3MiOiAiaHR0cHM6Ly9sb2dpbi5taWNyb3NvZnRvbmxpbmUuY29tL2Y2NDVhZDkyLWUzOGQtNGQxYS1iNTEwLWQxYjA5YTc0YThjYS92Mi4wIiwNCiAgImlhdCI6IDE1Mzg1Mzg0MjIsDQogICJuYmYiOiAxNTM4NTM4NDIyLA0KICAiZXhwIjogMTUzODU0MjMyMiwNCiAgIm5hbWUiOiAiQ2xvdWQgSURMQUIgQmFzaWMgVXNlciIsDQogICJvaWQiOiAiOWY0ODgwZDgtODBiYS00YzQwLTk3YmMtZjdhMjNjNzAzMDg0IiwNCiAgInByZWZlcnJlZF91c2VybmFtZSI6ICJpZGxhYkBtc2lkbGFiNC5vbm1pY3Jvc29mdC5jb20iLA0KICAic3ViIjogIlk2WWtCZEhOTkxITm1US2VsOUtoUno4d3Jhc3hkTFJGaVAxNEJSUFdybjQiLA0KICAidGlkIjogImY2NDVhZDkyLWUzOGQtNGQxYS1iNTEwLWQxYjA5YTc0YThjYSIsDQogICJ1dGkiOiAiNm5jaVgwMlNNa2k5azczLUYxc1pBQSIsDQogICJ2ZXIiOiAiMi4wIg0KfQ\u003d\u003d.e30\u003d"
22+
"AccessToken": {
23+
"an-entry": {
24+
"foo": "bar"
25+
},
26+
"uid.utid-login.example.com-accesstoken-my_client_id-contoso-s2 s1 s3": {
27+
"environment": "login.example.com",
28+
"credential_type": "AccessToken",
29+
"secret": "an access token",
30+
"realm": "contoso",
31+
"target": "s2 s1 s3",
32+
"client_id": "my_client_id",
33+
"cached_at": "1000",
34+
"home_account_id": "uid.utid",
35+
"extended_expires_on": "4600",
36+
"expires_on": "4600"
3337
}
3438
},
35-
"Account": {
36-
"9f4880d8-80ba-4c40-97bc-f7a23c703084.f645ad92-e38d-4d1a-b510-d1b09a74a8ca-login.microsoftonline.com-f645ad92-e38d-4d1a-b510-d1b09a74a8ca": {
37-
"home_account_id": "9f4880d8-80ba-4c40-97bc-f7a23c703084.f645ad92-e38d-4d1a-b510-d1b09a74a8ca",
38-
"environment": "login.microsoftonline.com",
39-
"realm": "f645ad92-e38d-4d1a-b510-d1b09a74a8ca",
40-
"local_account_id": "9f4880d8-80ba-4c40-97bc-f7a23c703084",
41-
"username": "[email protected]",
42-
"name": "Cloud IDLAB Basic User",
43-
"client_info": "eyJ1aWQiOiI5ZjQ4ODBkOC04MGJhLTRjNDAtOTdiYy1mN2EyM2M3MDMwODQiLCJ1dGlkIjoiZjY0NWFkOTItZTM4ZC00ZDFhLWI1MTAtZDFiMDlhNzRhOGNhIn0",
44-
"authority_type": "MSSTS"
39+
"IdToken": {
40+
"uid.utid-login.example.com-idtoken-my_client_id-contoso-": {
41+
"realm": "contoso",
42+
"environment": "login.example.com",
43+
"credential_type": "IdToken",
44+
"secret": "header.eyJvaWQiOiAib2JqZWN0MTIzNCIsICJwcmVmZXJyZWRfdXNlcm5hbWUiOiAiSm9obiBEb2UiLCAic3ViIjogInN1YiJ9.signature",
45+
"client_id": "my_client_id",
46+
"home_account_id": "uid.utid"
4547
}
4648
},
47-
"AppMetadata":{},
4849
"unknownEntity": {
4950
"field1": "1",
5051
"field2": "whats"
52+
},
53+
"AppMetadata": {
54+
"appmetadata-login.example.com-my_client_id": {
55+
"environment": "login.example.com",
56+
"family_id": null,
57+
"client_id": "my_client_id"
58+
}
5159
}
5260
}

0 commit comments

Comments
 (0)