RemoteAuthClient

public final class RemoteAuthClient


Provides a client for supporting remote authentication on Wear. The authentication session will be opened on the user's paired phone.

  • The following example triggers an authorization session to open on the phone.

// PKCE (Proof Key for Code Exchange) is required for the auth
private var codeVerifier: CodeVerifier
// Late initialization in place where it's used, or to be initialized in onCreate()
private var lateinit authClient: RemoteAuthClient

override public fun onDestroy() {
authClient.close();
super.onDestroy();
}

public fun startAuthFlow() {
// PKCE (Proof Key for Code Exchange) is required, store this code verifier here .
// To access the resource later, both the auth token ans code verifier are needed.
codeVerifier = CodeVerifier()

// Construct your auth request.
authClient = RemoteAuthClient.create(this);
authClient.sendAuthorizationRequest(
OAuthRequest.Builder(this.applicationContext)
.setAuthProviderUrl(Uri.parse("https://...."))
.setCodeChallenge(CodeChallenge(codeVerifier))
.build(),
Executors.newSingleThreadExecutor(),
new MyAuthCallback()
);
}

private class MyAuthCallback: RemoteAuthClient.Callback {
override public fun onAuthorizationResponse(
request: OAuthRequest,
response: OAuthResponse
) {
// Parse the result token out of the response and store it, e.g. in SharedPreferences,
// so you can use it later (Note, use together with code verifier from version R)
// You'll also want to display a success UI.
...
}

override fun onAuthorizationError(request: OAuthRequest, errorCode: Int) {
// Compare against codes available in RemoteAuthClient.ErrorCode
// You'll also want to display an error UI.
...
}
}

Summary

Nested types

RemoteAuthClient.Callback

This callback is notified when an async remote authentication request completes.

RemoteAuthClient.Companion

Public methods

@UiThread void

Frees any resources used by the client, dropping any outstanding requests.

final @UiThread void
sendAuthorizationRequest(
    OAuthRequest request,
    Executor executor,
    RemoteAuthClient.Callback clientCallback
)

Send a remote auth request.

Public methods

close

@UiThread
public void close()

Frees any resources used by the client, dropping any outstanding requests. The client cannot be used to make requests thereafter.

sendAuthorizationRequest

@UiThread
public final void sendAuthorizationRequest(
    OAuthRequest request,
    Executor executor,
    RemoteAuthClient.Callback clientCallback
)

Send a remote auth request. This will cause an authorization UI to be presented on the user's phone. This request is asynchronous; the callback provided will be be notified when the request completes.

Parameters
OAuthRequest request

Request that will be sent to the phone. The auth response should redirect to the Wear OS companion. See OAuthRequest.WEAR_REDIRECT_URL_PREFIX

Executor executor

The executor that callback will called on.

RemoteAuthClient.Callback clientCallback

The callback that will be notified when request is completed.

Throws
kotlin.RuntimeException

if the service has error to open the request