-
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.
Create controller & handler for google sign in logic #43
- Loading branch information
CMenne
committed
Jun 25, 2018
1 parent
6a9d9f0
commit 997b65d
Showing
3 changed files
with
111 additions
and
57 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
57 changes: 57 additions & 0 deletions
57
server/src/main/java/server/database/login/LoginController.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,57 @@ | ||
package server.database.login; | ||
|
||
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; | ||
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken; | ||
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier; | ||
import com.google.api.client.http.javanet.NetHttpTransport; | ||
import com.google.api.client.json.JsonFactory; | ||
import com.google.api.client.json.jackson2.JacksonFactory; | ||
import com.mongodb.client.MongoDatabase; | ||
import java.io.FileReader; | ||
import java.util.Collections; | ||
|
||
public class LoginController { | ||
|
||
// Construct controller for login. | ||
public LoginController() { | ||
|
||
} | ||
|
||
|
||
public String verifyIdToken(String idTokenString) { | ||
String CLIENT_SECRET_FILE = "./src/main/java/server/database/server_files/client_secret.json"; | ||
|
||
NetHttpTransport transport = new NetHttpTransport(); | ||
JsonFactory jsonFactory = new JacksonFactory(); | ||
String toReturn = "Failed to verify idToken"; | ||
|
||
try { | ||
GoogleClientSecrets clientSecrets = | ||
GoogleClientSecrets.load( | ||
JacksonFactory.getDefaultInstance(), new FileReader(CLIENT_SECRET_FILE)); | ||
|
||
GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(transport, jsonFactory) | ||
.setAudience(Collections.singletonList(clientSecrets.getDetails().getClientId())) | ||
.build(); | ||
|
||
GoogleIdToken idToken = verifier.verify(idTokenString); | ||
|
||
if (idToken != null) { | ||
GoogleIdToken.Payload payload = idToken.getPayload(); | ||
|
||
if (payload.getHostedDomain().equals("morris.umn.edu")) { | ||
toReturn = "Verified idToken"; | ||
} | ||
} else { | ||
System.out.println("Invalid ID token."); | ||
} | ||
} catch (Exception e) { | ||
System.out.println(e); | ||
|
||
toReturn = "null"; | ||
} | ||
|
||
|
||
return toReturn; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
server/src/main/java/server/database/login/LoginRequestHandler.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,47 @@ | ||
package server.database.login; | ||
|
||
import org.json.JSONObject; | ||
import spark.Request; | ||
import spark.Response; | ||
|
||
/** | ||
* | ||
*/ | ||
public class LoginRequestHandler { | ||
|
||
private final LoginController loginController; | ||
public LoginRequestHandler(LoginController loginController){ | ||
this.loginController = loginController; | ||
} | ||
|
||
/**Method called from Server when the 'api/login' endpoint is received. | ||
* Get a JSON response with a list of all the users in the database. | ||
* | ||
* @param req the HTTP request | ||
* @param res the HTTP response | ||
* @return one user in JSON formatted string and if it fails it will return text with a different HTTP status code | ||
*/ | ||
|
||
public String loginUser(Request req, Response res) { | ||
String idTokenString = getIdTokenString(req); | ||
String verifyResponse = loginController.verifyIdToken(idTokenString); | ||
|
||
if (!verifyResponse.equals("null")) { | ||
return verifyResponse; | ||
} else { | ||
return "null"; | ||
} | ||
|
||
} | ||
|
||
/**Method called from Server when the 'api/login' endpoint is received. | ||
* Get a JSON response with a list of all the users in the database. | ||
* | ||
* @param req the HTTP request | ||
* @return one user in JSON formatted string and if it fails it will return text with a different HTTP status code | ||
*/ | ||
|
||
private String getIdTokenString(Request req) { | ||
return new JSONObject(req.body()).getString("idToken"); | ||
} | ||
} |