Apple로 로그인 기능을 써보았다.
타 서비스는 oAuth2 로 연결하였으나 애플의 경우 직접 처리해본다!!!
순서는 이러하다.
1. front 로그인 버튼을 클릭하면 apple 로그인 화면이 뜬다.
2. 사용자가 로그인을 완료하면 개발자 센터에 등록해두었던 redirect url로 애플에서 데이터를 쏴준다.
3. Controller에서 애플에서 넘겨준 아래와 같은 데이터를 받는다.
{
state: 1111111
code: casq31253s211w35321sd15w3132e116w534s23
user: {"name":{"firstName":"Genie","lastName":"Park"},"email":"myemail@naver.com"}
}
4. 사용자가 최초로 로그인을 하였거나, 본인의 기기에서 애플로 로그인 기능을 지웠다가 다시 로그인하는 경우엔 user 데이터가 들어있다.
5. code 유효성 검사를 진행한다. 유효한 code라면 애플에서 아래와 같은 데이터를 넘겨준다.
{
"access_token": "qwe4q5w3e48qwe46q5we4q6w5e4qw.qw5e4q6we54qw",
"token_type": "Bearer",
"expires_in": 3600,
"refresh_token": "we4q5w3e48qwe46q5we4q6w5e4qw.qw5e4q6we54qwasd.asdasdasd",
"id_token": "asdasdasdasdasdasdasd3qwe4qw2.e1qwe654qw6e54q6w8e4q9w8ea.eqw5e6q4w8dq48q6wd4"
}
6. 넘겨준 id_token를 디코드하면 아래와 같은 정보를 얻을 수 있다.
{
"at_hash": "asd21a3sd8asd3554as3d",
"aud": "com.genie.service",
"sub": "00asd00.14qw0eqwe55.qwwwe410000",
"nonce_supported": true,
"email_verified": "true",
"auth_time": 1231231231,
"iss": "https://appleid.apple.com",
"exp": 123123123123,
"iat": 13123123123,
"email": "myemail@naver.com"
}
이렇게 받은 데이터를 가지고 내가 원하는 기능을 검증히고 구현하면 된다!!
코드로 봅시다!!!
1. 애플 개발자 센터 가입 후 아래의 키들을 발급받는다.
public static final String TEAM_ID = "팀아이디";
public static final String REDIRECT_URL = "애플에 등록한 리다이렉트 url"
public static final String CLIENT_ID = "Service ID 등록시 작성했던 Identifier";
public static final String KEY_ID = "MYKEY "; // .p8 파일을 다운받았다면 파일명에 적혀있다.
public static final String AUTH_URL = "https://appleid.apple.com";
public static final String KEY_PATH = "static/AuthKey_MYKEY.p8";
2. Front에 버튼을 생성한다.
<!doctype html>
<html lang="ko">
<head>
<meta charset="utf-8">
<title>Sign in with Apple </title>
<meta name="appleid-signin-client-id" content="서비스아이디 발급시 사용한 Identifier">
<meta name="appleid-signin-scope" content="name email"> <!-- 이름과 이메일을 받는다-->
<meta name="appleid-signin-redirect-uri" content="개발자센터에 등록한 리다이렉트 url">
<meta name="appleid-signin-state" content="임의의 문자열">
<meta name="appleid-signin-nonce" content="임의의 문자열">
<meta name="appleid-signin-use-popup" content="true"> <!-- or false defaults to false -->
</head>
<body>
<div id="appleid-signin" data-color="black" data-border="true" data-type="sign in"></div>
<script type="text/javascript" src="https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js"></script>
</html>
3. Controller 에서 redirect 부분을 작업해본다.
import java.net.URISyntaxException;
import java.text.ParseException;
import java.time.LocalDateTime;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.me.genie.util.AppleUtils;
import com.me.genie.util.HttpClientUtils;
@PostMapping("/auth/apple")
public CommonResponse<UserAuthForUserDto> authByApple(
HttpServletRequest request
, @RequestParam(value = "code", required= false) String code
, @RequestParam(value = "user", required= false) String user
, HttpServletResponse response) throws ParseException, JsonMappingException, JsonProcessingException, URISyntaxException {
/*********************************************************************
* 애플 로그인 취소시 null 리턴
* ******************************************************************/
if(code == null) return null;
/*********************************************************************
* code 유효성 검증
* 유효한 토큰이라면 id_token 이 포함된 데이터를 리턴해준다.
* ******************************************************************/
String apiResponse = AppleUtils.validateAuthorizationGrantCode(code);
if(apiResponse == null) {
logger.info("CODE 유효성 검사 실패");
return null;
}
/*********************************************************************
* id-token 유효성 검증
* ******************************************************************/
JSONObject tokenResponse = (JSONObject) JSONValue.parse(apiResponse);
if(!AppleUtils.verifyIdentityToken((String) tokenResponse.get("id_token"))) {
logger.info("ID-TOKEN 유효성 검사 실패");
return null;
}
/*********************************************************************
* id_token 토큰을 파싱하여 데이터를 가져온다.
* ******************************************************************/
JSONObject appleInfo = AppleUtils.decodeFromIdToken((String) tokenResponse.get("id_token"));
logger.debug(tokenResponse.toJSONString());
logger.debug(appleInfo.toJSONString());
/*********************************************************************
* user 정보는 최초 1회만 제공 받을 수 있다.
* 단, 고객이 본인의 아이디 관리에서 로그인을 해지한 경우엔 다시 받을 수 있다.
* ******************************************************************/
String username = "", email = (String) appleInfo.get("email");
User target = null;
if(user != null) { // 가입한적이 없거나, 애플로 로그인 기능을 삭제 후 다시 접근하는 경우.
// 사용자 정보를 추출한다.
JSONObject u = (JSONObject) JSONValue.parse(user);
username = (String) u.get("lastName") + (String) u.get("firstName");
email = (String) u.get("email");
// TODO: 여기서부터 자유롭게 로직을 진행하면 된다.
// 예) username과 email을 이용해 가입이력 조회
// 없다면 가입, 있다면 로그인 처리
} else {
// 기존에 가입한적이 있는 경우 여기로 들어온다.
if(email == null) {
return null;
}
// 기존에 가입한적이 있는 경우 refresh_token 유효성 검사를 진행한다.
if(AppleUtils.validateAnExistingRefreshToken((String) tokenResponse.get("refresh_token")) == null) {
logger.info("refresh_token 유효성 검사 실패");
return null;
}
if(email == null) {
return null;
}
/*********************************************************************
* 로그인 화면에서 사용자가 이메일을 공개로 로그인한 경우 이메일 값이 들어온다.
* 진짜 이메일이 들어오고 test111@naver.com
* 나의 이메일 숨기기를 선택했다면 email":"7ct4qm6gdn@privaterelay.appleid.com 와 같은 이메일이 들어온다.
* ******************************************************************/
email = (String) appleInfo.get("email");
// TODO: 여기서부터 자유롭게 로직을 진행하면 된다.
// 예) email or refesh_token을 통해 사용자 가입이력을 조회한뒤 처리한다.
// (refesh_token은 기한이 없음.)
}
4. Controller에서 사용한 Utils
package com.unli.cogba.util;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nimbusds.jose.*;
import com.nimbusds.jose.crypto.RSASSAVerifier;
import com.nimbusds.jose.jwk.JWK;
import com.nimbusds.jose.jwk.RSAKey;
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.SignedJWT;
import io.jsonwebtoken.JwsHeader;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.springframework.core.io.ClassPathResource;
import java.io.Reader;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.PrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class AppleUtils {
public static final String TEAM_ID = "team id";
public static final String REDIRECT_URL = "애플에 등록한 리다이렉트 url"
public static final String CLIENT_ID = "Service ID 등록시 작성했던 Identifier";
public static final String KEY_ID = "MYKEY "; // .p8 파일을 다운받았다면 파일명에 적혀있다.
public static final String AUTH_URL = "https://appleid.apple.com";
public static final String KEY_PATH = "static/AuthKey_MYKEY.p8";
/**
* createClientSecret 생성
* @return
*/
public static String createClientSecret() {
PrivateKey pKey = readPrivateKey(KEY_PATH);
String token = Jwts.builder()
.setHeaderParam(JwsHeader.KEY_ID, KEY_ID)
.setIssuer(TEAM_ID)
.setAudience("https://appleid.apple.com")
.setSubject(CLIENT_ID)
.setExpiration(new Date(System.currentTimeMillis() + (1000 * 60 * 5)))
.setIssuedAt(new Date(System.currentTimeMillis()))
.signWith(pKey, SignatureAlgorithm.ES256)
.compact();
return token;
}
/**
* 파일에서 private key 획득
* @return Private Key
*/
private static PrivateKey readPrivateKey(String keyPath) {
try {
ClassPathResource resource = new ClassPathResource(keyPath);
String privateKey = new String(Files.readAllBytes(Paths.get(resource.getURI())));
Reader pemReader = new StringReader(privateKey);
PEMParser pemParser = new PEMParser(pemReader);
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
PrivateKeyInfo object = (PrivateKeyInfo) pemParser.readObject();
return converter.getPrivateKey(object);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* id_token 검증
* @param id_token
* @return
*/
public static boolean verifyIdentityToken(String id_token) {
try {
SignedJWT signedJWT = SignedJWT.parse(id_token);
JWTClaimsSet payload = signedJWT.getJWTClaimsSet();
// EXP
Date currentTime = new Date(System.currentTimeMillis());
if (!currentTime.before(payload.getExpirationTime())) {
return false;
}
if (!"none에 입력했던값".equals(payload.getClaim("nonce")) || !AUTH_URL.equals(payload.getIssuer()) || !CLIENT_ID.equals(payload.getAudience().get(0))) {
return false;
}
// RSA
if (verifyPublicKey(signedJWT)) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 공개키 검증
* @param signedJWT
* @return
*/
private static boolean verifyPublicKey(SignedJWT signedJWT) {
try {
String publicKeys = HttpClientUtils.doGet("https://appleid.apple.com/auth/keys");
ObjectMapper objectMapper = new ObjectMapper();
Keys keys = objectMapper.readValue(publicKeys, Keys.class);
for (Key key : keys.getKeys()) {
RSAKey rsaKey = (RSAKey) JWK.parse(objectMapper.writeValueAsString(key));
RSAPublicKey publicKey = rsaKey.toRSAPublicKey();
JWSVerifier verifier = new RSASSAVerifier(publicKey);
if (signedJWT.verify(verifier)) {
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* refresh_token 유효성 검증
* @param client_secret
* @param refresh_token
* @return
*/
public static String validateAnExistingRefreshToken(String refresh_token) {
String reqUrl = AUTH_URL + "/auth/token";
Map<String, String> tokenRequest = new HashMap<>();
tokenRequest.put("client_id", CLIENT_ID);
tokenRequest.put("client_secret", createClientSecret());
tokenRequest.put("grant_type", "refresh_token");
tokenRequest.put("refresh_token", refresh_token);
return HttpClientUtils.doPost(reqUrl, tokenRequest);
}
/**
* code 유효성 검증
* @param code
* @return
*/
public static String validateAuthorizationGrantCode(String code) {
try {
String reqUrl = AUTH_URL + "/auth/token";
Map<String, String> tokenRequest = new HashMap<>();
tokenRequest.put("client_id", CLIENT_ID);
tokenRequest.put("client_secret", createClientSecret());
tokenRequest.put("code", code);
tokenRequest.put("grant_type", "authorization_code");
String apiResponse = HttpClientUtils.doPost(reqUrl, tokenRequest);
return apiResponse;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* id_token decode
* @param id_token
* @return
*/
public static JSONObject decodeFromIdToken(String id_token) {
try {
SignedJWT signedJWT = SignedJWT.parse(id_token);
JWTClaimsSet getPayload = signedJWT.getJWTClaimsSet();
String appleInfo = getPayload.toJSONObject().toJSONString();
JSONObject payload = (JSONObject) JSONValue.parse(appleInfo);
if (payload != null) {
return payload;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
5. 통신을 위한 util
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class HttpClientUtils {
private static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
private static ObjectMapper objectMapper = new ObjectMapper();
public static String doGet(String url) {
String result = null;
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
Integer statusCode = null;
String reasonPhrase = null;
try {
httpclient = HttpClients.createDefault();
HttpGet get = new HttpGet(url);
response = httpclient.execute(get);
statusCode = response.getStatusLine().getStatusCode();
reasonPhrase = response.getStatusLine().getReasonPhrase();
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
EntityUtils.consume(entity);
if (statusCode != 200) {
logger.error(String.format("[doGet]http get url(%s) failed. status code:%s. reason:%s. result:%s", url, statusCode, reasonPhrase, result));
}
} catch (Throwable t) {
logger.error(String.format("[doGet]http get url(%s) failed. status code:%s. reason:%s.", url, statusCode, reasonPhrase), t);
} finally {
try {
if (response != null) {
response.close();
}
if (httpclient != null) {
httpclient.close();
}
} catch (IOException e) {
logger.error(String.format("[doGet]release http get resource failed. url(%s). reason:%s.", url, e.getMessage()));
}
}
return result;
}
public static String doPost(String url, Map<String, String> param) {
String result = null;
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
Integer statusCode = null;
String reasonPhrase = null;
try {
httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
List<NameValuePair> nvps = new ArrayList<>();
Set<Entry<String, String>> entrySet = param.entrySet();
for (Entry<String, String> entry : entrySet) {
String fieldName = entry.getKey();
String fieldValue = entry.getValue();
nvps.add(new BasicNameValuePair(fieldName, fieldValue));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nvps);
httpPost.setEntity(formEntity);
response = httpclient.execute(httpPost);
statusCode = response.getStatusLine().getStatusCode();
reasonPhrase = response.getStatusLine().getReasonPhrase();
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
if (statusCode != 200) {
logger.error(String.format("[doPost]post url(%s) failed. status code:%s. reason:%s. param:%s. result:%s", url, statusCode, reasonPhrase, objectMapper.writeValueAsString(param), result));
}
EntityUtils.consume(entity);
} catch (Throwable t) {
try {
logger.error(String.format("[doPost]post url(%s) failed. status code:%s. reason:%s. param:%s.", url, statusCode, reasonPhrase, objectMapper.writeValueAsString(param)), t);
} catch (JsonProcessingException e) {
}
} finally {
try {
if (response != null) {
response.close();
}
if (httpclient != null) {
httpclient.close();
}
} catch (IOException e) {
try {
logger.error(String.format("[doPost]release http post resource failed. url(%s). reason:%s, param:%s.", url, e.getMessage(), objectMapper.writeValueAsString(param)));
} catch (JsonProcessingException ex) {
}
}
}
return result;
}
}
공개키 검증을 위한 utils
import lombok.Getter;
import lombok.Setter;
@Getter @Setter
public class Key {
private String kty;
private String kid;
private String use;
private String alg;
private String n;
private String e;
}
import java.util.List;
import lombok.Getter;
import lombok.Setter;
@Getter @Setter
public class Keys {
private List<Key> keys;
}
끝!!!!!!!!!!!!!!!!!!!!