generated from CS196Illinois/SP24-Project-Template
-
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 branch 'master' into matching_algo
- Loading branch information
Showing
14 changed files
with
343 additions
and
99 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
19 changes: 19 additions & 0 deletions
19
Project/backend/java-server/app/src/main/java/org/server/CheckInRoutes.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,19 @@ | ||
package org.server; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** This class represents the routes for check-in. */ | ||
@RestController | ||
public class CheckInRoutes { | ||
|
||
/** | ||
* Checks in the user. | ||
* | ||
* @return the response entity containing whether the check-in was successful | ||
*/ | ||
@GetMapping("/api/v1/checkin") | ||
public String checkIn() { | ||
return "Check-in successful!"; | ||
} | ||
} |
143 changes: 105 additions & 38 deletions
143
Project/backend/java-server/app/src/main/java/org/server/DiscordOps.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 |
---|---|---|
@@ -1,77 +1,144 @@ | ||
package org.server; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.github.scribejava.apis.DiscordApi; | ||
import com.github.scribejava.core.builder.ServiceBuilder; | ||
import com.github.scribejava.core.model.OAuth2AccessToken; | ||
import com.github.scribejava.core.model.OAuthRequest; | ||
import com.github.scribejava.core.model.Response; | ||
import com.github.scribejava.core.model.Verb; | ||
import com.github.scribejava.core.oauth.OAuth20Service; | ||
import io.github.cdimascio.dotenv.Dotenv; | ||
import io.mokulu.discord.oauth.DiscordAPI; | ||
import io.mokulu.discord.oauth.DiscordOAuth; | ||
import io.mokulu.discord.oauth.model.TokensResponse; | ||
import io.mokulu.discord.oauth.model.User; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.logging.Logger; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** This class represents the Discord operations. {@code @Author} adhit2 */ | ||
@Configuration | ||
public class DiscordOps { | ||
private static final Dotenv dotenv = Dotenv.load(); | ||
private static final String clientSecret = dotenv.get("DISCORD_CLIENT_SECRET"); | ||
private static final String clientId = dotenv.get("DISCORD_CLIENT_ID"); | ||
private static final String redirectUri = dotenv.get("DISCORD_REDIRECT_URI"); | ||
private static final String[] scope = {"identify"}; | ||
|
||
private static final PostgreSQLController pgController = new PostgreSQLController(); | ||
private final OAuth20Service service = | ||
new ServiceBuilder(Dotenv.load().get("DISCORD_CLIENT_ID")) | ||
.apiSecret(Dotenv.load().get("DISCORD_CLIENT_SECRET")) | ||
.callback(Dotenv.load().get("DISCORD_REDIRECT_URI")) | ||
.userAgent("ScribeJava") | ||
.build(DiscordApi.instance()); | ||
|
||
private static final String DISCORD_API_URL = "https://discord.com/api/v10"; | ||
private static final String PROTECTED_RESOURCE_URL = "https://discord.com/api/users/@me"; | ||
|
||
/** Constructs a new {@link DiscordOps}. */ | ||
public DiscordOps() {} | ||
|
||
private static final DiscordOAuth oauthHandler = | ||
new DiscordOAuth(clientId, clientSecret, redirectUri, scope); | ||
/** This class represents the Discord user. */ | ||
public static class DiscordUser { | ||
private String id; | ||
private String username; | ||
private String discriminator; | ||
|
||
@JsonProperty("global_name") | ||
private String globalName; | ||
|
||
// Getters and setters | ||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
} | ||
|
||
/** | ||
* Fetches the username. | ||
* Gets the username. | ||
* | ||
* @param discordId discord ID | ||
* @param discordId the discord ID | ||
* @return the username | ||
* @throws IOException if an error occurs | ||
*/ | ||
public static String getUsername(String discordId) throws IOException { | ||
String accessToken = pgController.getAccessToken(discordId); | ||
User user; | ||
public String getUsername(String discordId) | ||
throws IOException, ExecutionException, InterruptedException { | ||
try { | ||
DiscordAPI api = new DiscordAPI(accessToken); | ||
user = api.fetchUser(); | ||
} catch (IOException e) { | ||
// Token might be expired, refresh it | ||
pgController.changeAuthToken(discordId); | ||
accessToken = pgController.getAccessToken(discordId); | ||
user = new DiscordAPI(accessToken).fetchUser(); | ||
final OAuthRequest request = | ||
new OAuthRequest(Verb.GET, DISCORD_API_URL + "/users/" + discordId); | ||
service.signRequest(pgController.getAccessToken(discordId), request); | ||
Response response = service.execute(request); | ||
return response.getBody().split("\"username\":\"")[1].split("\"")[0]; | ||
} catch (Exception e) { | ||
refreshToken(pgController.getRefreshToken(discordId), discordId); | ||
final OAuthRequest request = | ||
new OAuthRequest(Verb.GET, DISCORD_API_URL + "/users/" + discordId); | ||
service.signRequest(pgController.getAccessToken(discordId), request); | ||
Response response = service.execute(request); | ||
return response.getBody().split("\"username\":\"")[1].split("\"")[0]; | ||
} | ||
return user.getUsername(); | ||
} | ||
|
||
/** | ||
* Gets the user info. | ||
* Refreshes the token. | ||
* | ||
* @param refreshToken refresh token | ||
* @return the user info | ||
* @throws IOException if an error occurs | ||
* @param refreshToken the refresh token | ||
* @param discordId the discord ID | ||
* @throws IOException if an I/O error occurs | ||
* @throws ExecutionException if an execution error occurs | ||
* @throws InterruptedException if an interrupt error occurs | ||
*/ | ||
public static String refreshToken(String refreshToken) throws IOException { | ||
TokensResponse tokensResponse = oauthHandler.refreshTokens(refreshToken); | ||
return tokensResponse.getAccessToken(); | ||
public void refreshToken(String refreshToken, String discordId) | ||
throws IOException, ExecutionException, InterruptedException { | ||
final OAuth2AccessToken accessToken = service.refreshAccessToken(refreshToken); | ||
pgController.changeAuthToken( | ||
accessToken.getAccessToken(), pgController.getDiscordId(refreshToken)); | ||
} | ||
|
||
/** | ||
* Gets the tokens. | ||
* | ||
* @param code the code | ||
* @return the tokens | ||
* @throws IOException if an error occurs | ||
* @throws IOException if an I/O error occurs | ||
* @throws ExecutionException if an execution error occurs | ||
* @throws InterruptedException if an interrupt error occurs | ||
*/ | ||
public static TokensResponse getTokens(String code) throws IOException { | ||
return oauthHandler.getTokens(code); | ||
public OAuth2AccessToken getTokens(String code) | ||
throws IOException, ExecutionException, InterruptedException { | ||
return service.getAccessToken(code); | ||
} | ||
|
||
/** | ||
* Gets the discord ID. | ||
* | ||
* @param accessToken the access token | ||
* @return the discord ID | ||
* @throws IOException if an error occurs | ||
* @throws IOException if an I/O error occurs | ||
* @throws ExecutionException if an execution error occurs | ||
* @throws InterruptedException if an interrupt error occurs | ||
*/ | ||
public static String getDiscordId(String accessToken) throws IOException { | ||
return new DiscordAPI(accessToken).fetchUser().getId(); | ||
public String getDiscordId(String accessToken) | ||
throws IOException, ExecutionException, InterruptedException { | ||
try { | ||
return getString(accessToken); | ||
} catch (Exception e) { | ||
String discordId = pgController.getDiscordId(accessToken); | ||
refreshToken(pgController.getRefreshToken(discordId), discordId); | ||
return getString(accessToken); | ||
} | ||
} | ||
|
||
private String getString(String accessToken) | ||
throws InterruptedException, ExecutionException, IOException { | ||
final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); | ||
service.signRequest(accessToken, request); | ||
Response response = service.execute(request); | ||
Logger.getGlobal().info(response.getBody()); | ||
return response.getBody().split("\"id\":\"")[1].split("\"")[0]; | ||
} | ||
} |
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
Oops, something went wrong.