Skip to content

Commit

Permalink
Changed access tokens to use pennmobile API
Browse files Browse the repository at this point in the history
  • Loading branch information
JSnipes29 committed Mar 31, 2023
1 parent 5b74a33 commit d11f7e6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,27 +198,28 @@ class LoginWebviewFragment : Fragment() {
}

private fun initiateAuthentication(authCode: String) {
mPlatform?.getAccessToken(authCode,

"authorization_code", clientID, redirectUri, codeVerifier,
object : Callback<AccessTokenResponse> {

override fun success(t: AccessTokenResponse?, response: Response?) {
if (response?.status == 200) {
val accessToken = t?.accessToken
val editor = sp.edit()
editor.putString(getString(R.string.access_token), accessToken)
editor.putString(getString(R.string.refresh_token), t?.refreshToken)
editor.putString(getString(R.string.expires_in), t?.expiresIn)

val expiresInInt = t?.expiresIn!!.toInt() * 1000
Log.i("LoginWebview", "Expires In: $expiresInInt")
val currentTime = Calendar.getInstance().timeInMillis
editor.putLong(getString(R.string.token_expires_at), currentTime + expiresInInt)
editor.apply()
getUser(accessToken)
}
mStudentLife.getAccessToken(authCode,

"authorization_code", clientID, redirectUri, codeVerifier,
object : Callback<AccessTokenResponse> {

override fun success(t: AccessTokenResponse?, response: Response?) {
if (response?.status == 200) {
val accessToken = t?.accessToken
val editor = sp.edit()
editor.putString(getString(R.string.access_token), accessToken)
editor.putString(getString(R.string.refresh_token), t?.refreshToken)
editor.putString(getString(R.string.expires_in), t?.expiresIn)

val expiresInInt = t?.expiresIn!!.toInt() * 1000
Log.i("LoginWebview", "Expires In: $expiresInInt")
val currentTime = Calendar.getInstance().timeInMillis
editor.putLong(getString(R.string.token_expires_at), currentTime + expiresInInt)
editor.apply()
getUser(accessToken)
}
}

override fun failure(error: RetrofitError) {
Log.e("Accounts", "Error fetching access token $error", error)
Toast.makeText(mActivity, "Error logging in", Toast.LENGTH_SHORT).show()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import java.util.*

class OAuth2NetworkManager(private var mActivity: MainActivity) {

private var mPlatform = MainActivity.platformInstance
private var mStudentLife = MainActivity.studentLifeInstance
private val sp = PreferenceManager.getDefaultSharedPreferences(mActivity)
val editor = sp?.edit()

Expand Down Expand Up @@ -43,7 +43,7 @@ class OAuth2NetworkManager(private var mActivity: MainActivity) {
val refreshToken = sp.getString(mActivity.getString(R.string.refresh_token), "")
val clientID = BuildConfig.PLATFORM_CLIENT_ID

mPlatform.refreshAccessToken(refreshToken,
mStudentLife.refreshAccessToken(refreshToken,
"refresh_token", clientID,
object : Callback<AccessTokenResponse> {

Expand Down Expand Up @@ -92,7 +92,7 @@ class OAuth2NetworkManager(private var mActivity: MainActivity) {
val refreshToken = sp.getString(mActivity.getString(R.string.refresh_token), "")
val clientID = BuildConfig.PLATFORM_CLIENT_ID

mPlatform.refreshAccessToken(refreshToken,
mStudentLife.refreshAccessToken(refreshToken,
"refresh_token", clientID,
object : Callback<AccessTokenResponse> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.pennapps.labs.pennmobile.classes.GSRBookingResult;
import com.pennapps.labs.pennmobile.classes.GSRLocation;
import com.pennapps.labs.pennmobile.classes.GSRReservation;
import com.pennapps.labs.pennmobile.classes.GetUserResponse;
import com.pennapps.labs.pennmobile.classes.Gym;
import com.pennapps.labs.pennmobile.classes.HomeCell;
import com.pennapps.labs.pennmobile.classes.LaundryRequest;
Expand Down Expand Up @@ -45,6 +46,17 @@
* Retrofit interface to the Penn Mobile API
*/
public interface StudentLife {

@FormUrlEncoded
@POST("/accounts/token/")
void getAccessToken(
@Field("code") String authCode,
@Field("grant_type") String grantType,
@Field("client_id") String clientID,
@Field("redirect_uri") String redirectURI,
@Field("code_verifier") String codeVerifier,
Callback<AccessTokenResponse> callback);

@FormUrlEncoded
@POST("/accounts/token/")
void refreshAccessToken(
Expand Down

0 comments on commit d11f7e6

Please sign in to comment.