Skip to content

Commit

Permalink
#5 feat: client_secret 생성 - 암호화 알고리즘에 맞는 별도의 라이브러리 사용
Browse files Browse the repository at this point in the history
  • Loading branch information
shinhn committed Oct 27, 2022
1 parent 4eaec31 commit 487b325
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ plugins {

group = 'com.yogit'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
sourceCompatibility = '17'
targetCompatibility = '17'

configurations {
compileOnly {
Expand Down Expand Up @@ -41,5 +42,5 @@ tasks.named('test') {
compileJava.options.compilerArgs.addAll([
"--add-exports=java.base/sun.security.pkcs=ALL-UNNAMED",
"--add-exports=java.base/sun.security.util=ALL-UNNAMED",
"--add-exports=java.base/sun.security.x509=ALL-UNNAMED",
"--add-exports=java.base/sun.security.x509=ALL-UNNAMED"
])
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.security.NoSuchAlgorithmException;
import java.util.Map;

@Controller
Expand Down Expand Up @@ -68,7 +69,7 @@ public String appleLogin(ModelMap model) {
*/
@PostMapping(value = "/redirect")
@ResponseBody
public TokenResponse servicesRedirect(ServicesResponse serviceResponse) {
public TokenResponse servicesRedirect(ServicesResponse serviceResponse) throws NoSuchAlgorithmException {

System.out.println("1------------");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import com.yogit.server.applelogin.model.TokenResponse;

import java.security.NoSuchAlgorithmException;
import java.util.Map;

public interface AppleService {

String getAppleClientSecret(String id_token);
String getAppleClientSecret(String id_token) throws NoSuchAlgorithmException;

TokenResponse requestCodeValidations(String client_secret, String code, String refresh_token);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.util.Map;

@Service
Expand All @@ -20,7 +21,7 @@ public class AppleServiceImpl implements AppleService {
* @return
*/
@Override
public String getAppleClientSecret(String id_token) {
public String getAppleClientSecret(String id_token) throws NoSuchAlgorithmException {

if (appleUtils.verifyIdentityToken(id_token)) {
return appleUtils.createClientSecret();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@

import java.io.FileReader;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -126,7 +129,7 @@ private boolean verifyPublicKey(SignedJWT signedJWT) {
*
* @return client_secret(jwt)
*/
public String createClientSecret() {
public String createClientSecret() throws NoSuchAlgorithmException {

JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.ES256).keyID(KEY_ID).build();
JWTClaimsSet claimsSet = new JWTClaimsSet();
Expand All @@ -140,16 +143,28 @@ public String createClientSecret() {

SignedJWT jwt = new SignedJWT(header, claimsSet);

// try {
// ECPrivateKey ecPrivateKey = new ECPrivateKeyImpl2(readPrivateKey());
// JWSSigner jwsSigner = new ECDSASigner(ecPrivateKey.getS());
//
// jwt.sign(jwsSigner);
//
// } catch (InvalidKeyException e) {
// e.printStackTrace();
// } catch (JOSEException e) {
// e.printStackTrace();
// }

PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(readPrivateKey());
KeyFactory kf = KeyFactory.getInstance("EC");
try {
ECPrivateKey ecPrivateKey = new ECPrivateKeyImpl2(readPrivateKey());
ECPrivateKey ecPrivateKey = (ECPrivateKey) kf.generatePrivate(spec);
JWSSigner jwsSigner = new ECDSASigner(ecPrivateKey.getS());

jwt.sign(jwsSigner);

} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (JOSEException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
throw new RuntimeException(e);
}

return jwt.serialize();
Expand Down
2 changes: 1 addition & 1 deletion server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ spring:
jpa:
database: mysql
hibernate:
ddl-auto: create
ddl-auto: update

0 comments on commit 487b325

Please sign in to comment.