Skip to content

Commit 4fee1bc

Browse files
committed
update dependencies
- also fix a resulting conflicting dependency problem from google-api-client; - fix some typos in README
1 parent 27d9890 commit 4fee1bc

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Next, you have to configure your web page for Sign in with Apple. Follow the gui
1414
# Example
1515
## Using `AppleAuthProvider.java` `AppleClientPrivateKeyFactory.java`
1616
First order of business should be creating an instance of `ECPrivateKey` representing the client's(your) private key.<br/>
17-
`AppleKeyProvider` can help you create a `ECPrivateKey` if you have your private key as string or stream (from a p8 for example).
17+
`AppleClientPrivateKeyFactory` can help you create a `ECPrivateKey` if you have your private key as string or stream (from a p8 for example).
1818

1919
Creating a new instance of `AppleAuthProvider`, should be trivial at this point. The only two parameters that are not
2020
self explanatory are the `SecretGenerator` and the collection of scopes.<br/>
@@ -30,7 +30,7 @@ Once you have your `AppleAuthProvider` instance you can:
3030
## Handling initial response from Apple
3131
After the user clicks on the "Sign in with Apple" button on your page they will be redirected to https://appleid.apple.com/.
3232
After they provide their credentials Apple will make a POST request to the url that you have specified as Redirect URL.
33-
It will contain a ```code``` field. Its contents is what should be handed down to `makeNewAuthorisationTokenRequest` in order retrieve thee authorization token (it will also contain the state used to create the redirect url).
33+
It will contain a ```code``` field. Its contents is what should be handed down to `makeNewAuthorisationTokenRequest` in order retrieve the authorization token (it will also contain the state used to create the redirect url).
3434
Keep in mind that tokens returned from Apple are short-lived, so you should create a session or a user in your system
3535
using the returned ```AppleAuthorizationToken``` object. After that you can verify if the user is
3636
still logged in using "Sign in with Apple" by retrieving a refresh token using the ```makeNewRefreshTokenRequest``` method.
@@ -44,14 +44,14 @@ still logged in using "Sign in with Apple" by retrieving a refresh token using t
4444
private static final String REDIRECT_URL = "Your redirect url";
4545

4646
public static void main(String[] args) throws IOException, InvalidKeySpecException {
47-
//Generating your private key.
48-
//This could be just a string containing the key.
47+
// Generating your private key.
48+
// This could be just a string containing the key.
4949
InputStream pkStream = AppleIdTokenManager.class
5050
.getClassLoader().getResourceAsStream("your_pk_file.p8");
5151
AppleClientPrivateKeyFactory appleClientPrivateKeyFactory = new AppleClientPrivateKeyFactory();
5252
ECPrivateKey privateKey = appleClientPrivateKeyFactory.getEcPrivateKey(pkStream);
5353

54-
//Creating provider instance.
54+
// Creating provider instance.
5555
SecretGenerator secretGenerator = new SecretGenerator();
5656
AppleAuthProvider appleAuthProvider = new AppleAuthProvider(
5757
CLIENT_ID,
@@ -63,20 +63,20 @@ still logged in using "Sign in with Apple" by retrieving a refresh token using t
6363
REDIRECT_URL
6464
);
6565

66-
//We are ready to start using the provider.
66+
// We are ready to start using the provider.
6767

68-
//Generate a url and navigate the user to it.
68+
// Generate a url and navigate the user to it.
6969
String loginLink = appleAuthProvider.getLoginLink("Some form of state");
7070

71-
//Once the user is redirected back to our domain get the "code" in the request.
71+
// Once the user is redirected back to our domain get the "code" in the request.
7272
String authCode = "the code in the callback request";
73-
//Now we can authenticate the user.
73+
// Now we can authenticate the user.
7474
AppleAuthorizationToken initialToken = appleAuthProvider.makeNewAuthorisationTokenRequest(authCode);
75-
//After the authentication we should check (not more than once every once 24 hours) if the user still
76-
// logged in using "Sign in with Apple" by retrieving a refresh token.
75+
// After the authentication we should check (not more than once every 24h) if the user
76+
// is still logged in using "Sign in with Apple" by retrieving a refresh token.
7777
AppleAuthorizationToken refreshToken = appleAuthProvider.makeNewRefreshTokenRequest(initialToken
7878
.getRefreshToken());
7979

8080
}
8181
}
82-
```
82+
```

pom.xml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,20 @@
8484
<dependency>
8585
<groupId>com.auth0</groupId>
8686
<artifactId>java-jwt</artifactId>
87-
<version>3.10.3</version>
87+
<version>4.0.0</version>
8888
</dependency>
8989

9090
<dependency>
9191
<groupId>com.auth0</groupId>
9292
<artifactId>jwks-rsa</artifactId>
93-
<version>0.11.0</version>
93+
<version>0.21.1</version>
9494
</dependency>
9595

9696
<dependency>
9797
<groupId>com.google.api-client</groupId>
9898
<artifactId>google-api-client</artifactId>
99-
<version>1.30.9</version>
99+
<version>2.0.0</version>
100100
</dependency>
101101
</dependencies>
102102

103-
104-
</project>
103+
</project>

src/main/java/com/accedia/apple/auth/AppleAuthProvider.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import com.google.api.client.http.HttpTransport;
1313
import com.google.api.client.http.javanet.NetHttpTransport;
1414
import com.google.api.client.json.JsonFactory;
15-
import com.google.api.client.json.jackson2.JacksonFactory;
15+
import com.google.api.client.json.gson.GsonFactory;
1616
import com.google.common.base.Supplier;
1717
import com.google.common.base.Suppliers;
1818
import com.google.common.collect.ImmutableList;
@@ -26,7 +26,8 @@
2626
import java.util.List;
2727
import java.util.Optional;
2828
import java.util.concurrent.TimeUnit;
29-
import java.util.stream.Collectors;
29+
30+
import static java.util.stream.Collectors.toList;
3031

3132
public class AppleAuthProvider {
3233

@@ -45,7 +46,7 @@ public class AppleAuthProvider {
4546
private final long secretLifeInSec;
4647
private final GenericUrl appleAuthTokenUrl;
4748
private final String appleAuthAuthorizationUrl;
48-
private UserDataDeserializer userDataDeserializer;
49+
private final UserDataDeserializer userDataDeserializer;
4950
private final List<String> appleUserScopes;
5051
private final String redirectUrl;
5152
private final HttpTransport httpTransport;
@@ -81,11 +82,11 @@ public AppleAuthProvider(String clientId, String keyId, String teamId, SecretGen
8182
DEFAULT_APPLE_AUTH_AUTHORIZE_URL,
8283
new UserDataDeserializer(),
8384
new NetHttpTransport(),
84-
new JacksonFactory(),
85+
new GsonFactory(),
8586
DEFAULT_MAX_TIMEOUT_IN_SEC,
8687
secretGenerator,
8788
privateKey,
88-
() -> Instant.now(),
89+
Instant::now,
8990
DEFAULT_SECRET_LIFE_IN_SEC,
9091
new AppleKeyProvider(),
9192
scopes,
@@ -146,7 +147,7 @@ public AppleAuthProvider(String clientId,
146147
this.secretLifeInSec = secretLifeInSec;
147148
this.userDataDeserializer = userDataDeserializer;
148149
this.appleUserScopes = scopes != null ? ImmutableList.copyOf(
149-
scopes.stream().map(AppleUserScope::getLiteral).collect(Collectors.toList())
150+
scopes.stream().map(AppleUserScope::getLiteral).collect(toList())
150151
) : null;
151152
this.appleAuthTokenUrl = new GenericUrl(appleAuthTokenUrl);
152153
this.jsonFactory = jsonFactory;
@@ -159,7 +160,7 @@ public AppleAuthProvider(String clientId,
159160
this.httpTransport = httpTransport;
160161

161162
Algorithm validationAlg = Algorithm.RSA256(appleKeyProvider);
162-
this.jwtVerifier = JWT.require(validationAlg)
163+
this.jwtVerifier = JWT.require(validationAlg)
163164
.build();
164165

165166
}
@@ -182,13 +183,13 @@ public String getLoginLink(String state) {
182183
.setResponseTypes(Arrays.asList("code", "id_token"))
183184
.setScopes(appleUserScopes)
184185
.setState(state)
185-
.set("response_mode", "form_post")//Could be parameterized based on scope.
186+
.set("response_mode", "form_post") //Could be parameterized based on scope.
186187
.build();
187188
}
188189

189190
/**
190-
* Makes an authorisation request. Retrieves an User's date from Apple. Use this object to create users or sessions.
191-
* @param authCode Received from Apple after successfully redirecting the use.
191+
* Makes an authorisation request. Retrieves a User's date from Apple. Use this object to create users or sessions.
192+
* @param authCode Received from Apple after successfully redirecting the user.
192193
* @throws IOException
193194
*/
194195
public AppleAuthorizationToken makeNewAuthorisationTokenRequest(String authCode) throws IOException {
@@ -201,7 +202,7 @@ public AppleAuthorizationToken makeNewAuthorisationTokenRequest(String authCode)
201202
/**
202203
* Verifies if a token is valid.
203204
* Use this method to check daily if the user is still signed in on your app using Apple ID.
204-
* @param refreshToken
205+
* @param refreshToken
205206
* @return
206207
* @throws IOException
207208
*/
@@ -218,13 +219,13 @@ private AppleAuthorizationToken executeTokenRequest(TokenRequest tokenRequest) t
218219
TokenResponse tokenResponse = tokenRequest.execute();
219220
Optional<String> idToken = Optional.ofNullable(tokenResponse.get("id_token")).map(Object::toString);
220221
idToken.ifPresent(this::validateToken);
221-
Optional<UserData> userData = idToken.map(userDataDeserializer::getUserDataFromIdToken);
222+
UserData userData = idToken.map(userDataDeserializer::getUserDataFromIdToken).orElse(null);
222223
return new AppleAuthorizationToken(
223224
tokenResponse.getAccessToken(),
224225
tokenResponse.getExpiresInSeconds(),
225226
idToken.orElse(null),
226227
tokenResponse.getRefreshToken(),
227-
userData.orElse(null));
228+
userData);
228229
}
229230

230231
private void validateToken(String token) {

0 commit comments

Comments
 (0)