Skip to content

Commit

Permalink
feat(authentication): add revokeAccessToken(...) method (#644)
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz committed Jun 17, 2024
1 parent f597638 commit 9cca701
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 45 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-feet-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capacitor-firebase/authentication': minor
---

feat: add `revokeAccessToken(...)` method
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@
import io.capawesome.capacitorjs.plugins.firebase.authentication.classes.SignInResult;
import io.capawesome.capacitorjs.plugins.firebase.authentication.classes.SignInWithPhoneNumberOptions;
import io.capawesome.capacitorjs.plugins.firebase.authentication.classes.options.FetchSignInMethodsForEmailOptions;
import io.capawesome.capacitorjs.plugins.firebase.authentication.classes.options.RevokeAccessTokenOptions;
import io.capawesome.capacitorjs.plugins.firebase.authentication.classes.results.FetchSignInMethodsForEmailResult;
import io.capawesome.capacitorjs.plugins.firebase.authentication.handlers.AppleAuthProviderHandler;
import io.capawesome.capacitorjs.plugins.firebase.authentication.handlers.FacebookAuthProviderHandler;
import io.capawesome.capacitorjs.plugins.firebase.authentication.handlers.GoogleAuthProviderHandler;
import io.capawesome.capacitorjs.plugins.firebase.authentication.handlers.OAuthProviderHandler;
import io.capawesome.capacitorjs.plugins.firebase.authentication.handlers.PhoneAuthProviderHandler;
import io.capawesome.capacitorjs.plugins.firebase.authentication.handlers.PlayGamesAuthProviderHandler;
import io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces.EmptyResultCallback;
import io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces.NonEmptyResultCallback;
import io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces.Result;
import io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces.ResultCallback;
import java.util.Arrays;
Expand Down Expand Up @@ -123,7 +126,7 @@ public void confirmPasswordReset(@NonNull String oobCode, @NonNull String newPas
);
}

public void confirmVerificationCode(@NonNull ConfirmVerificationCodeOptions options, @NonNull ResultCallback callback) {
public void confirmVerificationCode(@NonNull ConfirmVerificationCodeOptions options, @NonNull NonEmptyResultCallback callback) {
phoneAuthProviderHandler.confirmVerificationCode(options, callback);
}

Expand All @@ -137,7 +140,7 @@ public void deleteUser(FirebaseUser user, @NonNull Runnable callback) {
);
}

public void fetchSignInMethodsForEmail(FetchSignInMethodsForEmailOptions options, @NonNull final ResultCallback resultCallback) {
public void fetchSignInMethodsForEmail(FetchSignInMethodsForEmailOptions options, @NonNull final NonEmptyResultCallback callback) {
String email = options.getEmail();

getFirebaseAuthInstance()
Expand All @@ -147,10 +150,10 @@ public void fetchSignInMethodsForEmail(FetchSignInMethodsForEmailOptions options
if (task.isSuccessful()) {
List<String> signInMethods = task.getResult().getSignInMethods();
FetchSignInMethodsForEmailResult result = new FetchSignInMethodsForEmailResult(signInMethods);
resultCallback.success(result);
callback.success(result);
} else {
Exception exception = task.getException();
resultCallback.error(exception);
callback.error(exception);
}
}
);
Expand All @@ -161,10 +164,10 @@ public FirebaseUser getCurrentUser() {
return getFirebaseAuthInstance().getCurrentUser();
}

public void getIdToken(Boolean forceRefresh, @NonNull final ResultCallback resultCallback) {
public void getIdToken(Boolean forceRefresh, @NonNull final NonEmptyResultCallback callback) {
FirebaseUser user = getCurrentUser();
if (user == null) {
resultCallback.error(new Exception(FirebaseAuthenticationPlugin.ERROR_NO_USER_SIGNED_IN));
callback.error(new Exception(FirebaseAuthenticationPlugin.ERROR_NO_USER_SIGNED_IN));
return;
}
Task<GetTokenResult> tokenResultTask = user.getIdToken(forceRefresh);
Expand All @@ -173,10 +176,10 @@ public void getIdToken(Boolean forceRefresh, @NonNull final ResultCallback resul
if (task.isSuccessful()) {
String token = task.getResult().getToken();
GetIdTokenResult result = new GetIdTokenResult(token);
resultCallback.success(result);
callback.success(result);
} else {
Exception exception = task.getException();
resultCallback.error(exception);
callback.error(exception);
}
}
);
Expand Down Expand Up @@ -315,6 +318,15 @@ public void reload(FirebaseUser user, @NonNull Runnable callback) {
);
}

public void revokeAccessToken(@NonNull RevokeAccessTokenOptions options, @NonNull EmptyResultCallback callback) {
String token = options.getToken();

getFirebaseAuthInstance()
.revokeAccessToken(token)
.addOnSuccessListener(unused -> callback.success())
.addOnFailureListener(exception -> callback.error(exception));
}

public void sendEmailVerification(FirebaseUser user, @NonNull Runnable callback) {
user
.sendEmailVerification()
Expand Down Expand Up @@ -626,7 +638,11 @@ public void handleOnActivityResult(int requestCode, int resultCode, @NonNull Int
}
}

public void signInWithCredential(@NonNull SignInOptions options, @NonNull AuthCredential credential, @NonNull ResultCallback callback) {
public void signInWithCredential(
@NonNull SignInOptions options,
@NonNull AuthCredential credential,
@NonNull NonEmptyResultCallback callback
) {
boolean skipNativeAuth = options.getSkipNativeAuth();
if (skipNativeAuth) {
SignInResult result = new SignInResult(null, credential, null);
Expand All @@ -651,7 +667,7 @@ public void signInWithCredential(@NonNull SignInOptions options, @NonNull AuthCr
);
}

public void linkWithCredential(@NonNull AuthCredential credential, @NonNull ResultCallback callback) {
public void linkWithCredential(@NonNull AuthCredential credential, @NonNull NonEmptyResultCallback callback) {
FirebaseUser user = getFirebaseAuthInstance().getCurrentUser();
if (user == null) {
callback.error(new Exception(FirebaseAuthenticationPlugin.ERROR_NO_USER_SIGNED_IN));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
import io.capawesome.capacitorjs.plugins.firebase.authentication.classes.SignInResult;
import io.capawesome.capacitorjs.plugins.firebase.authentication.classes.SignInWithPhoneNumberOptions;
import io.capawesome.capacitorjs.plugins.firebase.authentication.classes.options.FetchSignInMethodsForEmailOptions;
import io.capawesome.capacitorjs.plugins.firebase.authentication.classes.options.RevokeAccessTokenOptions;
import io.capawesome.capacitorjs.plugins.firebase.authentication.handlers.FacebookAuthProviderHandler;
import io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces.EmptyResultCallback;
import io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces.NonEmptyResultCallback;
import io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces.Result;
import io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces.ResultCallback;
import org.json.JSONObject;
Expand All @@ -34,6 +37,7 @@ public class FirebaseAuthenticationPlugin extends Plugin {
public static final String PHONE_CODE_SENT_EVENT = "phoneCodeSent";
public static final String ERROR_PROVIDER_ID_MISSING = "providerId must be provided.";
public static final String ERROR_NO_USER_SIGNED_IN = "No user is signed in.";
public static final String ERROR_TOKEN_MISSING = "token must be provided.";
public static final String ERROR_OOB_CODE_MISSING = "oobCode must be provided.";
public static final String ERROR_TENANT_ID_MISSING = "tenantId must be provided.";
public static final String ERROR_EMAIL_MISSING = "email must be provided.";
Expand Down Expand Up @@ -118,7 +122,7 @@ public void confirmVerificationCode(PluginCall call) {
return;
}
ConfirmVerificationCodeOptions options = new ConfirmVerificationCodeOptions(verificationId, verificationCode);
ResultCallback callback = new ResultCallback() {
NonEmptyResultCallback callback = new NonEmptyResultCallback() {
@Override
public void success(Result result) {
call.resolve(result.toJSObject());
Expand Down Expand Up @@ -177,23 +181,21 @@ public void fetchSignInMethodsForEmail(PluginCall call) {
}

FetchSignInMethodsForEmailOptions options = new FetchSignInMethodsForEmailOptions(email);
NonEmptyResultCallback callback = new NonEmptyResultCallback() {
@Override
public void success(Result result) {
call.resolve(result.toJSObject());
}

implementation.fetchSignInMethodsForEmail(
options,
new ResultCallback() {
@Override
public void success(Result result) {
call.resolve(result.toJSObject());
}

@Override
public void error(Exception exception) {
Logger.error(TAG, exception.getMessage(), exception);
String code = FirebaseAuthenticationHelper.createErrorCode(exception);
call.reject(exception.getMessage(), code);
}
@Override
public void error(Exception exception) {
Logger.error(TAG, exception.getMessage(), exception);
String code = FirebaseAuthenticationHelper.createErrorCode(exception);
call.reject(exception.getMessage(), code);
}
);
};

implementation.fetchSignInMethodsForEmail(options, callback);
} catch (Exception exception) {
Logger.error(TAG, exception.getMessage(), exception);
String code = FirebaseAuthenticationHelper.createErrorCode(exception);
Expand Down Expand Up @@ -221,22 +223,21 @@ public void getIdToken(PluginCall call) {
try {
Boolean forceRefresh = call.getBoolean("forceRefresh", false);

implementation.getIdToken(
forceRefresh,
new ResultCallback() {
@Override
public void success(Result result) {
call.resolve(result.toJSObject());
}

@Override
public void error(Exception exception) {
Logger.error(TAG, exception.getMessage(), exception);
String code = FirebaseAuthenticationHelper.createErrorCode(exception);
call.reject(exception.getMessage(), code);
}
NonEmptyResultCallback callback = new NonEmptyResultCallback() {
@Override
public void success(Result result) {
call.resolve(result.toJSObject());
}
);

@Override
public void error(Exception exception) {
Logger.error(TAG, exception.getMessage(), exception);
String code = FirebaseAuthenticationHelper.createErrorCode(exception);
call.reject(exception.getMessage(), code);
}
};

implementation.getIdToken(forceRefresh, callback);
} catch (Exception exception) {
Logger.error(TAG, exception.getMessage(), exception);
String code = FirebaseAuthenticationHelper.createErrorCode(exception);
Expand Down Expand Up @@ -457,6 +458,38 @@ public void reload(PluginCall call) {
}
}

@PluginMethod
public void revokeAccessToken(PluginCall call) {
try {
String token = call.getString("token");
if (token == null) {
call.reject(ERROR_TOKEN_MISSING);
return;
}

RevokeAccessTokenOptions options = new RevokeAccessTokenOptions(token);
EmptyResultCallback callback = new EmptyResultCallback() {
@Override
public void success() {
call.resolve();
}

@Override
public void error(Exception exception) {
Logger.error(TAG, exception.getMessage(), exception);
String code = FirebaseAuthenticationHelper.createErrorCode(exception);
call.reject(exception.getMessage(), code);
}
};

implementation.revokeAccessToken(options, callback);
} catch (Exception exception) {
Logger.error(TAG, exception.getMessage(), exception);
String code = FirebaseAuthenticationHelper.createErrorCode(exception);
call.reject(exception.getMessage(), code);
}
}

@PluginMethod
public void sendEmailVerification(PluginCall call) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.capawesome.capacitorjs.plugins.firebase.authentication.classes.options;

import androidx.annotation.NonNull;

public class RevokeAccessTokenOptions {

@NonNull
private String token;

public RevokeAccessTokenOptions(@NonNull String token) {
this.token = token;
}

@NonNull
public String getToken() {
return this.token;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.capawesome.capacitorjs.plugins.firebase.authentication.classes.SignInOptions;
import io.capawesome.capacitorjs.plugins.firebase.authentication.classes.SignInResult;
import io.capawesome.capacitorjs.plugins.firebase.authentication.classes.SignInWithPhoneNumberOptions;
import io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces.NonEmptyResultCallback;
import io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces.Result;
import io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces.ResultCallback;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -44,7 +45,7 @@ public void link(@NonNull final LinkWithPhoneNumberOptions options) throws Excep
verifyPhoneNumber(options, true);
}

public void confirmVerificationCode(@NonNull ConfirmVerificationCodeOptions options, @NonNull ResultCallback callback) {
public void confirmVerificationCode(@NonNull ConfirmVerificationCodeOptions options, @NonNull NonEmptyResultCallback callback) {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(options.getVerificationId(), options.getVerificationCode());
if (signInOnConfirm) {
pluginImplementation.signInWithCredential(new SignInOptions(skipNativeAuthOnConfirm), credential, callback);
Expand Down Expand Up @@ -77,7 +78,7 @@ private PhoneAuthProvider.OnVerificationStateChangedCallbacks createCallbacks(
return new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
ResultCallback callback = new ResultCallback<SignInResult>() {
NonEmptyResultCallback callback = new NonEmptyResultCallback<SignInResult>() {
@Override
public void success(SignInResult result) {
PhoneVerificationCompletedEvent event = new PhoneVerificationCompletedEvent(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces;

public interface EmptyResultCallback extends ResultCallback {
void success();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces;

import androidx.annotation.NonNull;

public interface NonEmptyResultCallback<T extends Result> extends ResultCallback {
void success(@NonNull T result);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.capawesome.capacitorjs.plugins.firebase.authentication.interfaces;

public interface ResultCallback<T extends Result> {
void success(T result);
public interface ResultCallback {
void error(Exception exception);
}
4 changes: 4 additions & 0 deletions packages/authentication/ios/Plugin.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
7C579FFF29F3FCF200B7778B /* LinkWithPhoneNumberOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C579FFE29F3FCF200B7778B /* LinkWithPhoneNumberOptions.swift */; };
7C57A00129F4023E00B7778B /* RuntimeError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C57A00029F4023E00B7778B /* RuntimeError.swift */; };
7C8E530826656FD100FF4B87 /* GoogleAuthProviderHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C8E530726656FD100FF4B87 /* GoogleAuthProviderHandler.swift */; };
7C939DAD2C208CD30059B145 /* RevokeAccessTokenOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C939DAC2C208CD30059B145 /* RevokeAccessTokenOptions.swift */; };
7CC1EA4F26B41A370022F1B9 /* FirebaseAuthenticationConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CC1EA4E26B41A370022F1B9 /* FirebaseAuthenticationConfig.swift */; };
7CD588F02688B4EF002BDF65 /* FirebaseAuthenticationHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CD588EF2688B4EF002BDF65 /* FirebaseAuthenticationHelper.swift */; };
7CD588F22688B5A4002BDF65 /* OAuthProviderHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CD588F12688B5A4002BDF65 /* OAuthProviderHandler.swift */; };
Expand Down Expand Up @@ -73,6 +74,7 @@
7C579FFE29F3FCF200B7778B /* LinkWithPhoneNumberOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkWithPhoneNumberOptions.swift; sourceTree = "<group>"; };
7C57A00029F4023E00B7778B /* RuntimeError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RuntimeError.swift; sourceTree = "<group>"; };
7C8E530726656FD100FF4B87 /* GoogleAuthProviderHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoogleAuthProviderHandler.swift; sourceTree = "<group>"; };
7C939DAC2C208CD30059B145 /* RevokeAccessTokenOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RevokeAccessTokenOptions.swift; sourceTree = "<group>"; };
7CC1EA4E26B41A370022F1B9 /* FirebaseAuthenticationConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirebaseAuthenticationConfig.swift; sourceTree = "<group>"; };
7CD588EF2688B4EF002BDF65 /* FirebaseAuthenticationHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirebaseAuthenticationHelper.swift; sourceTree = "<group>"; };
7CD588F12688B5A4002BDF65 /* OAuthProviderHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OAuthProviderHandler.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -157,6 +159,7 @@
isa = PBXGroup;
children = (
7C0C050A2BAD80E2004252BE /* FetchSignInMethodsForEmailOptions.swift */,
7C939DAC2C208CD30059B145 /* RevokeAccessTokenOptions.swift */,
);
path = Options;
sourceTree = "<group>";
Expand Down Expand Up @@ -442,6 +445,7 @@
7CD588F02688B4EF002BDF65 /* FirebaseAuthenticationHelper.swift in Sources */,
7C0C050D2BAD80EC004252BE /* FetchSignInMethodsForEmailResult.swift in Sources */,
7C579FFB29F3FBD000B7778B /* SignInOptions.swift in Sources */,
7C939DAD2C208CD30059B145 /* RevokeAccessTokenOptions.swift in Sources */,
7C461B0529EC430D00DD31E9 /* Result.swift in Sources */,
50E1A94820377CB70090CE1A /* FirebaseAuthenticationPlugin.swift in Sources */,
7CD588F42688B6A7002BDF65 /* AppleAuthProviderHandler.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation

@objc public class RevokeAccessTokenOptions: NSObject {
private var token: String

init(token: String) {
self.token = token
}

func getToken() -> String {
return token
}
}
Loading

0 comments on commit 9cca701

Please sign in to comment.