-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from Kek-i/feature/8-users-auth
- Loading branch information
Showing
7 changed files
with
247 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/com/codepatissier/keki/user/dto/GoogleLogin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.codepatissier.keki.user.dto; | ||
|
||
import com.github.scribejava.core.builder.api.DefaultApi20; | ||
|
||
public class GoogleLogin extends DefaultApi20{ | ||
|
||
protected GoogleLogin(){ | ||
} | ||
|
||
private static class InstanceHolder{ | ||
private static final GoogleLogin INSTANCE = new GoogleLogin(); | ||
} | ||
|
||
public static GoogleLogin instance(){ | ||
return InstanceHolder.INSTANCE; | ||
} | ||
|
||
@Override | ||
public String getAccessTokenEndpoint() { | ||
return "https://www.googleapis.com/oauth2/v4/token"; | ||
} | ||
|
||
@Override | ||
protected String getAuthorizationBaseUrl() { | ||
return "https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&prompt=consent"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,5 @@ | |
|
||
@Getter | ||
public enum Provider { | ||
GOOGLE, KAKAO, NAVER | ||
GOOGLE, KAKAO, NAVER, ANONYMOUS | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/main/java/com/codepatissier/keki/user/service/GoogleService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package com.codepatissier.keki.user.service; | ||
|
||
import com.codepatissier.keki.user.dto.GoogleLogin; | ||
import com.github.scribejava.core.builder.ServiceBuilder; | ||
import com.github.scribejava.core.oauth.OAuth20Service; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonParser; | ||
import com.nimbusds.jose.shaded.json.parser.ParseException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.servlet.http.HttpSession; | ||
import java.io.*; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class GoogleService { | ||
|
||
@Value("${google.client-id}") | ||
private String client_id; | ||
|
||
@Value("${google.redirect_uri}") | ||
private String redirect_uri; | ||
|
||
@Value("${google.client-secret}") | ||
private String client_secret; | ||
|
||
private final static String USER_INFO_URL = "https://www.googleapis.com/userinfo/v2/me?access_token="; | ||
private final static String ACCESS_TOKEN_URL = "https://www.googleapis.com/oauth2/v4/token"; | ||
|
||
public String getAuthorizationUrl(HttpSession session) { | ||
OAuth20Service oauthService = new ServiceBuilder() | ||
.apiKey(client_id) | ||
.apiSecret(client_secret) | ||
.callback(redirect_uri) | ||
.scope("email") | ||
.build(GoogleLogin.instance()); | ||
|
||
return oauthService.getAuthorizationUrl(); | ||
} | ||
|
||
public String getAccessToken(HttpSession session, String authorize_code) throws IOException { | ||
String access_Token = ""; | ||
|
||
try { | ||
URL url = new URL(ACCESS_TOKEN_URL); | ||
|
||
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | ||
|
||
conn.setRequestMethod("POST"); | ||
conn.setDoOutput(true); | ||
|
||
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream())); | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("grant_type=authorization_code"); | ||
|
||
sb.append("&client_id="+client_id); | ||
sb.append("&client_secret="+client_secret); | ||
sb.append("&redirect_uri="+redirect_uri); | ||
sb.append("&code="+authorize_code); | ||
bw.write(sb.toString()); | ||
bw.flush(); | ||
|
||
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); | ||
String line = ""; | ||
String result = ""; | ||
|
||
while ((line = br.readLine()) != null) { | ||
result += line; | ||
} | ||
|
||
JsonParser parser = new JsonParser(); | ||
JsonElement element = parser.parse(result); | ||
access_Token = element.getAsJsonObject().get("access_token").getAsString(); | ||
br.close(); | ||
bw.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return access_Token; | ||
} | ||
|
||
public String getUserInfo(String access_Token) throws IOException, ParseException { | ||
String reqURL = USER_INFO_URL+access_Token; | ||
try { | ||
URL url = new URL(reqURL); | ||
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | ||
conn.setRequestProperty("Authorization", "Bearer " + access_Token); | ||
|
||
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); | ||
String line = ""; | ||
String result = ""; | ||
while ((line = br.readLine()) != null) { | ||
result += line; | ||
} | ||
JsonParser parser = new JsonParser(); | ||
JsonElement element = parser.parse(result); | ||
String email = element.getAsJsonObject().get("email").getAsString(); | ||
return email; | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters