use of com.instructure.canvasapi.model.OAuthToken in project instructure-android by instructure.
the class OAuthWebLogin method setupCallback.
public void setupCallback() {
mobileVerifyCallback = new CanvasCallback<DomainVerificationResult>(APIHelpers.statusDelegateWithContext(OAuthWebLogin.this)) {
@Override
public void cache(DomainVerificationResult domainVerificationResult) {
}
@Override
public void firstPage(DomainVerificationResult domainVerificationResult, LinkHeaders linkHeaders, Response response) {
if (domainVerificationResult.getResult() == DomainVerificationResult.DomainVerificationCode.Success) {
// Domain is now verified.
// save domain to the preferences.
String domain = "";
// mobile verify can change the hostname we need to use
if (domainVerificationResult.getBase_url() != null && !domainVerificationResult.getBase_url().equals("")) {
domain = domainVerificationResult.getBase_url();
} else {
domain = url;
}
// The domain gets set afterwards in SetUpInstance, but domain is required a bit before that works.
APIHelpers.setDomain(OAuthWebLogin.this, domain);
client_id = domainVerificationResult.getClient_id();
client_secret = domainVerificationResult.getClient_secret();
// Get the protocol
api_protocol = domainVerificationResult.getProtocol();
// Set the protocol
APIHelpers.setProtocol(domainVerificationResult.getProtocol(), OAuthWebLogin.this);
// Get device name for the login request.
String deviceName = Build.MODEL;
if (deviceName == null || deviceName.equals("")) {
deviceName = getString(R.string.unknownDevice);
}
// Remove spaces
deviceName = deviceName.replace(" ", "_");
// changed for the online update to have an actual formatted login page
authenticationURL = api_protocol + "://" + domain + "/login/oauth2/auth?client_id=" + client_id + "&response_type=code&redirect_uri=urn:ietf:wg:oauth:2.0:oob&mobile=1";
authenticationURL += "&purpose=" + deviceName;
if (canvas_login == 1) {
authenticationURL += "&canvas_login=1";
} else if (canvas_login == 2) {
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setCookie(api_protocol + "://" + domain, "canvas_sa_delegated=1");
}
web.loadUrl(authenticationURL);
} else {
// Error message
int errorId;
if (domainVerificationResult.getResult() == DomainVerificationResult.DomainVerificationCode.GeneralError) {
errorId = R.string.mobileVerifyGeneral;
} else if (domainVerificationResult.getResult() == DomainVerificationResult.DomainVerificationCode.DomainNotAuthorized) {
errorId = R.string.mobileVerifyDomainUnauthorized;
} else if (domainVerificationResult.getResult() == DomainVerificationResult.DomainVerificationCode.UnknownUserAgent) {
errorId = R.string.mobileVerifyUserAgentUnauthorized;
} else {
errorId = R.string.mobileVerifyUnknownError;
}
MaterialDialog.Builder builder = new MaterialDialog.Builder(OAuthWebLogin.this);
builder.title(R.string.errorOccurred);
builder.content(errorId);
builder.cancelable(true);
builder.positiveColor(Color.BLACK);
MaterialDialog dialog = builder.build();
dialog.show();
}
}
};
getToken = new CanvasCallback<OAuthToken>(APIHelpers.statusDelegateWithContext(OAuthWebLogin.this)) {
@Override
public void cache(OAuthToken oAuthToken, LinkHeaders linkHeaders, Response response) {
}
@Override
public void firstPage(OAuthToken oAuthToken, LinkHeaders linkHeaders, Response response) {
// Set up the rest adapter and such.
APIHelpers.setToken(getContext(), oAuthToken.getAccess_token());
CanvasRestAdapter.setupInstance(getContext(), oAuthToken.getAccess_token(), APIHelpers.loadProtocol(getContext()) + "://" + APIHelpers.getDomain(getContext()));
// save the successful domain to be remembered for later
JSONArray domains = SavedDomains.getSavedDomains(OAuthWebLogin.this, prefNamePreviousDomain);
String domain = APIHelpers.getDomain(OAuthWebLogin.this);
domains.put(domain);
// save the new domain
SavedDomains.setSavedDomains(OAuthWebLogin.this, domains, prefNamePreviousDomain);
// Set the last used domain.
setLastSignedInDomain(domain, OAuthWebLogin.this);
// We now need to get the cache user
UserAPI.getSelf(new UserCallback(APIHelpers.statusDelegateWithContext(OAuthWebLogin.this)) {
@Override
public void cachedUser(User user) {
}
@Override
public void user(User user, Response response) {
Intent intent = OAuthWebLogin.this.getIntent();
intent.putExtra(URLSignIn.loggedInIntent, true);
if (passedURI != null) {
intent.putExtra(Const.PASSED_URI, passedURI);
}
OAuthWebLogin.this.setResult(RESULT_OK, intent);
OAuthWebLogin.this.finish();
}
});
}
@Override
public boolean onFailure(RetrofitError retrofitError) {
if (!specialCase) {
Toast.makeText(OAuthWebLogin.this, R.string.errorOccurred, Toast.LENGTH_SHORT).show();
} else {
specialCase = false;
}
web.loadUrl(authenticationURL);
return true;
}
};
}
Aggregations