Adding Apple Sign In To Spring Boot App (JAVA) Backend part

Tags: java, applesignin, sociallogin, oauth
Published: 2020-07-27 11:14:56

So we submitted our app to IOS Store for review and it got rejected because of one silly reason. Because we were using other social logins and not apple login, so they simply asked us to replace other logins with Apple. Does not that sound bad ? Yeah, we also felt same, and asked but they were strict on this. So , we have to add. But there was a very few docs and only some blogs on medium here with some different languages and not java. So struggling for over a week. I was finally able to done this, so i am now sharing it here, so you all do not face same issues.
Some ideas taken from previous blogs, listed below. Go check.

{% medium https://medium.com/jeff-tech/sign-in-with-apple-id-backend-part-java-70dc9aa2c9a %}

{%medium https://medium.com/jeff-tech/sign-in-apple-id-42655b0cce2f %}


## 1. Generate following from Developer account
* Apple Certificate (.p8), that needs to be downloaded
* Apple Client ID
* Apple Key ID
* Apple Team ID
* Apple URL (https://appleid.apple.com)

These all can be generated by going to keys and identifiers menu in Application Developer account of apple

## 2. Generating a JWT Token for Apple which will work as Client_Secret for apple authentication

{% gist https://gist.github.com/balvinder294/c5939b5732ab79d09ba53ef23e978e5a %}

## 3. Authorize request method to get OAuth token as TokenResponse and ID token as ID Token Payload

> Note : Apple only sends user obj first time as a JSON string containing email and name. If user not available, email can be get from idToken

{% gist https://gist.github.com/balvinder294/3c9b3c90f8ec72484d78f54a79ee04af %}

## 4. Response classes
{% gist https://gist.github.com/balvinder294/72a437791aab7c9708b5a74dcece41b9 %}

{% gist https://gist.github.com/balvinder294/344284456e06c37d5afcdf08b8381092 %}

{% gist https://gist.github.com/balvinder294/8a6c8c4754c309a3a7052ed037bc3c3b %}

So, i have listed the methods to complete the authorization and get details in backend. I will next add a new post for frontend login also, using some more java code. Let me know if any thing you stuck on.

<br>

<hr>

## Bonus code

Let me share here some more code below as a bonus, which will be used with frontend for web authorization flow. I will share frontend code later.
1 Method to generate authorization url with params. Also as we have multiple subdomains in project, adding the state as subDomain for redirecting to subdomain later.

> Note: Advantage of this method is that, by using a single domain and redirect url, we can achieve Apple sign(Any other social Sign in also) for multiple domains using popup, to catch authorization params in the url after redirection.
i will add code for popup later in frontend part

{% gist https://gist.github.com/balvinder294/abaf08910affb08314f032127d4790b8 %}

2 Method for handling redirect from form_post to get redirect with authorization params as query params in popup window url.

{% gist https://gist.github.com/balvinder294/6672de5fa77b83674643733d9f10086f %}

So, this is it the secret bonus method, many people still looking for.


<hr>

So this was all for now, i hope it will help you in achieving apple sign in. There are some more issues you may face, that i will address in later posts. You can comment any issue you may face and i will try to help as much as i can. Feel free to like and show love, your support inspires me to write more and share like this. Thanks for reading.

> Do visit our another technical Blog [Tekraze.com](Tekraze.com) for more posts

> Originally published at Medium

{% medium https://medium.com/tekraze/adding-apple-sign-in-to-spring-boot-app-java-backend-part-e053da331a %}