Skip to content

Commit

Permalink
반환을 Map이 아니라 전용객체로 반환하게끔 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
dolong2 committed Jan 20, 2023
1 parent ec55885 commit 399b022
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/main/java/gauth/GAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.simple.JSONObject;
Expand All @@ -25,33 +24,34 @@ private enum Auth{
ACCESS,
REFRESH
}
public static Map<String, String> generateToken(String email, String password, String clientId, String clientSecret, String redirectUri) throws IOException {
String code = generateCode(email, password);
return getToken(code, clientId, clientSecret, redirectUri);
public static GAuthToken generateToken(String email, String password, String clientId, String clientSecret, String redirectUri) throws IOException {
String code = generateCode(email, password).getCode();
return new GAuthToken(getToken(code, clientId, clientSecret, redirectUri));
}

public static Map<String, String> generateToken(String code, String clientId, String clientSecret, String redirectUri) throws IOException {
return getToken(code, clientId, clientSecret, redirectUri);
public static GAuthToken generateToken(String code, String clientId, String clientSecret, String redirectUri) throws IOException {
return new GAuthToken(getToken(code, clientId, clientSecret, redirectUri));
}

public static String generateCode(String email, String password) throws IOException {
public static GAuthCode generateCode(String email, String password) throws IOException {
Map<String, String> body = new HashMap<>();
body.put("email", email);
body.put("password", password);
String code = sendPostGAuthServer(body, null, "/code").get("code");
return code;
return new GAuthCode(code);
}

public static Map<String, String> refresh(String refreshToken) throws IOException{
public static GAuthToken refresh(String refreshToken) throws IOException{
if(!refreshToken.startsWith("Bearer "))
refreshToken = "Bearer "+refreshToken;
return sendPatchGAuthServer(null, refreshToken, "/token", Auth.REFRESH);
return new GAuthToken(sendPatchGAuthServer(null, refreshToken, "/token", Auth.REFRESH));
}

public static Map<String, String> getUserInfo(String accessToken) throws IOException{
public static GAuthUserInfo getUserInfo(String accessToken) throws IOException{
if(!accessToken.startsWith("Bearer "))
accessToken = "Bearer "+accessToken;
return sendGetResourceServer(accessToken, "/user");
Map<String, Object> map = sendGetResourceServer(accessToken, "/user");
return new GAuthUserInfo(map);
}

private static Map<String, String> getToken(String code, String clientId, String clientSecret, String redirectUri) throws IOException {
Expand All @@ -71,11 +71,11 @@ private static Map<String, String> sendPatchGAuthServer(Map<String, String> body
return sendPatch(body, token, GAuthServerURL+ url, auth);
}

private static Map<String, String> sendGetResourceServer(String token, String url) throws IOException {
private static Map<String, Object> sendGetResourceServer(String token, String url) throws IOException {
return sendGet(token, ResourceServerURL + url);
}

private static Map<String, String> sendGet(String token, String url) throws IOException {
private static Map<String, Object> sendGet(String token, String url) throws IOException {
HttpGet request = new HttpGet(url); //GET 메소드 URL 생성
request.addHeader("Authorization", token);
CloseableHttpClient client = HttpClientBuilder.create().build();
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/gauth/GAuthCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package gauth;

public class GAuthCode {
private String code;

public GAuthCode(String code) {
this.code = code;
}

public String getCode() {
return code;
}
}
21 changes: 21 additions & 0 deletions src/main/java/gauth/GAuthToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package gauth;

import java.util.Map;

public class GAuthToken {
private String accessToken;
private String refreshToken;

public GAuthToken(Map<String, String> map) {
this.accessToken = map.get("accessToken");
this.refreshToken = map.get("refreshToken");
}

public String getAccessToken() {
return accessToken;
}

public String getRefreshToken() {
return refreshToken;
}
}
57 changes: 57 additions & 0 deletions src/main/java/gauth/GAuthUserInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package gauth;

import java.util.Map;

public class GAuthUserInfo {
private String email;
private String name;
private Integer grade;
private Integer classNum;
private Integer num;
private String gender;
private String profileUrl;
private String role;

public GAuthUserInfo(Map<String, Object> map) {
this.email = (String) map.get("email");
this.name = (String) map.get("name");
this.grade = (Integer) map.get("grade");
this.classNum = (Integer) map.get("classNum");
this.num = (Integer) map.get("num");
this.gender = (String) map.get("gender");
this.profileUrl = (String) map.get("profileUrl");
this.role = (String) map.get("role");
}

public String getEmail() {
return email;
}

public String getName() {
return name;
}

public Integer getGrade() {
return grade;
}

public Integer getClassNum() {
return classNum;
}

public Integer getNum() {
return num;
}

public String getGender() {
return gender;
}

public String getProfileUrl() {
return profileUrl;
}

public String getRole() {
return role;
}
}

0 comments on commit 399b022

Please sign in to comment.