Skip to content

Instantly share code, notes, and snippets.

@akeemphilbert
Last active August 6, 2018 14:46
Show Gist options
  • Save akeemphilbert/78c55fddfce25a9653f2 to your computer and use it in GitHub Desktop.
Save akeemphilbert/78c55fddfce25a9653f2 to your computer and use it in GitHub Desktop.
Example of using a Custom Authorizer with Couchbase Lite in Java
package com.domain.example
import com.couchbase.lite.Database;
import com.couchbase.lite.auth.Authorizer;
import com.couchbase.lite.util.Log;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by Akeem Philbert <[email protected]> on 4/7/14.
*/
public class CustomAuthorizer extends Authorizer {
private String emailAddress;
private static final String AUTHENTICATE_ENDPOINT = "/some/endpoint";
private static Map<String, String> accessTokens;
public static final String LOGIN_PARAMETER_ACCESS_TOKEN = "variable_name";
public CustomAuthorizer(String emailAddress) {
this.emailAddress = emailAddress;
}
public boolean usesCookieBasedLogin() {
return true;
}
public String loginPathForSite(URL site) {
return AUTHENTICATE_ENDPOINT;
}
public Map<String, String> loginParametersForSite(URL site) {
Map<String, String> loginParameters = new HashMap<String, String>();
try {
String accessToken = accessTokenForEmailAndSite(this.emailAddress, site);
if (accessToken != null) {
loginParameters.put(LOGIN_PARAMETER_ACCESS_TOKEN, accessToken);
return loginParameters;
} else {
return null;
}
} catch (Exception e) {
Log.e(Database.TAG, "Error looking login parameters for site", e);
}
return null;
}
public synchronized static String registerAccessToken(String accessToken, String email) {
if (accessTokens == null) {
accessTokens = new HashMap<String, String>();
}
Log.d(Database.TAG, "CustomAuthorizer registering key: " + email);
accessTokens.put(email, accessToken);
return email;
}
public static String accessTokenForEmailAndSite(String email, URL site) {
try {
Log.d(Database.TAG, "CustomAuthorizer looking up key: " + email + " from list of access tokens");
return accessTokens.get(email);
} catch (Exception e) {
Log.e(Database.TAG, "Error looking up access token", e);
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment