Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the support for custom headers in both android and ios #226

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 42 additions & 34 deletions android/src/main/java/io/fullstack/oauth/OAuthManagerModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ public String getName() {

@ReactMethod
public void configureProvider(
final String providerName,
final ReadableMap params,
final String providerName,
final ReadableMap params,
@Nullable final Callback onComplete
) {
Log.i(TAG, "configureProvider for " + providerName);

// Save callback url for later
String callbackUrlStr = params.getString("callback_url");
_callbackUrls.add(callbackUrlStr);

Log.d(TAG, "Added callback url " + callbackUrlStr + " for providler " + providerName);

// Keep configuration map
Expand Down Expand Up @@ -104,9 +104,9 @@ public void configureProvider(

@ReactMethod
public void authorize(
final String providerName,
@Nullable final ReadableMap params,
final Callback callback)
final String providerName,
@Nullable final ReadableMap params,
final Callback callback)
{
try {
final OAuthManagerModule self = this;
Expand All @@ -115,7 +115,7 @@ public void authorize(
Activity activity = this.getCurrentActivity();
FragmentManager fragmentManager = activity.getFragmentManager();
String callbackUrl = "http://localhost/" + providerName;

OAuthManagerOnAccessTokenListener listener = new OAuthManagerOnAccessTokenListener() {
public void onRequestTokenError(final Exception ex) {
Log.e(TAG, "Exception with request token: " + ex.getMessage());
Expand All @@ -139,7 +139,7 @@ public void onOAuth2AccessToken(final OAuth2AccessToken accessToken) {
};

if (authVersion.equals("1.0")) {
final OAuth10aService service =
final OAuth10aService service =
OAuthManagerProviders.getApiFor10aProvider(providerName, cfg, params, callbackUrl);

OAuthManagerFragmentController ctrl =
Expand All @@ -149,7 +149,7 @@ public void onOAuth2AccessToken(final OAuth2AccessToken accessToken) {
} else if (authVersion.equals("2.0")) {
final OAuth20Service service =
OAuthManagerProviders.getApiFor20Provider(providerName, cfg, params, callbackUrl);

OAuthManagerFragmentController ctrl =
new OAuthManagerFragmentController(mReactContext, fragmentManager, providerName, service, callbackUrl);

Expand All @@ -165,9 +165,9 @@ public void onOAuth2AccessToken(final OAuth2AccessToken accessToken) {

@ReactMethod
public void makeRequest(
final String providerName,
final String providerName,
final String urlString,
final ReadableMap params,
final ReadableMap params,
final Callback onComplete) {

Log.i(TAG, "makeRequest called for " + providerName + " to " + urlString);
Expand All @@ -190,7 +190,7 @@ public void makeRequest(
}

String httpMethod;
if (params.hasKey("method")) {
if (params.hasKey("method")) {
httpMethod = params.getString("method");
} else {
httpMethod = "GET";
Expand All @@ -215,19 +215,27 @@ public void makeRequest(
httpVerb = Verb.TRACE;
} else {
httpVerb = Verb.GET;
}
}

ReadableMap requestParams = null;
if (params != null && params.hasKey("params")) {
requestParams = params.getMap("params");
}
OAuthRequest request = oauthRequestWithParams(providerName, cfg, authVersion, httpVerb, url, requestParams);

if (params != null && params.hasKey("headers")) {
HashMap<String, String> headers = (HashMap<String, String>) params.toHashMap().get("headers");

for (Map.Entry<String, String> item : headers.entrySet()) {
request.addHeader(item.getKey(), item.getValue());
}
}

if (authVersion.equals("1.0")) {
final OAuth10aService service =
final OAuth10aService service =
OAuthManagerProviders.getApiFor10aProvider(providerName, cfg, requestParams, null);
OAuth1AccessToken token = _credentialsStore.get(providerName, OAuth1AccessToken.class);

service.signRequest(token, request);
} else if (authVersion.equals("2.0")) {
final OAuth20Service service =
Expand All @@ -244,7 +252,7 @@ public void makeRequest(
onComplete.invoke(err);
return;
}

final Response response = request.send();
final String rawBody = response.getBody();

Expand All @@ -255,7 +263,7 @@ public void makeRequest(
resp.putInt("status", response.getCode());
resp.putString("data", rawBody);
onComplete.invoke(null, resp);

} catch (IOException ex) {
Log.e(TAG, "IOException when making request: " + ex.getMessage());
ex.printStackTrace();
Expand All @@ -277,18 +285,18 @@ private OAuthRequest oauthRequestWithParams(
OAuthRequest request;
// OAuthConfig config;

if (authVersion.equals("1.0")) {
// final OAuth10aService service =
if (authVersion.equals("1.0")) {
// final OAuth10aService service =
// OAuthManagerProviders.getApiFor10aProvider(providerName, cfg, null, null);
OAuth1AccessToken oa1token = _credentialsStore.get(providerName, OAuth1AccessToken.class);
request = OAuthManagerProviders.getRequestForProvider(
providerName,
providerName,
httpVerb,
oa1token,
oa1token,
url,
cfg,
params);

// config = service.getConfig();
// request = new OAuthRequest(httpVerb, url.toString(), config);
} else if (authVersion.equals("2.0")) {
Expand All @@ -298,13 +306,13 @@ private OAuthRequest oauthRequestWithParams(

OAuth2AccessToken oa2token = _credentialsStore.get(providerName, OAuth2AccessToken.class);
request = OAuthManagerProviders.getRequestForProvider(
providerName,
providerName,
httpVerb,
oa2token,
oa2token,
url,
cfg,
params);

// config = service.getConfig();
// request = new OAuthRequest(httpVerb, url.toString(), config);
} else {
Expand All @@ -322,8 +330,8 @@ public void getSavedAccounts(final ReadableMap options, final Callback onComplet

@ReactMethod
public void getSavedAccount(
final String providerName,
final ReadableMap options,
final String providerName,
final ReadableMap options,
final Callback onComplete)
{
try {
Expand All @@ -343,7 +351,7 @@ public void getSavedAccount(
onComplete.invoke(null, resp);
} else if (authVersion.equals("2.0")) {
OAuth2AccessToken token = _credentialsStore.get(providerName, OAuth2AccessToken.class);

if (token == null || token.equals("")) {
throw new Exception("No token found");
}
Expand All @@ -360,7 +368,7 @@ public void getSavedAccount(
ex.printStackTrace();
exceptionCallback(ex, onComplete);
}

}

@ReactMethod
Expand Down Expand Up @@ -429,7 +437,7 @@ private WritableMap accessTokenResponse(
String uuid = accessToken.getParameter("user_id");
response.putString("uuid", uuid);
String oauthTokenSecret = (String) accessToken.getParameter("oauth_token_secret");

String tokenType = (String) accessToken.getParameter("token_type");
if (tokenType == null) {
tokenType = "Bearer";
Expand Down Expand Up @@ -465,18 +473,18 @@ private WritableMap accessTokenResponse(

String uuid = accessToken.getParameter("user_id");
response.putString("uuid", uuid);

WritableMap credentials = Arguments.createMap();
Log.d(TAG, "Credential raw response: " + accessToken.getRawResponse());

credentials.putString("accessToken", accessToken.getAccessToken());
String authHeader;

String tokenType = accessToken.getTokenType();
if (tokenType == null) {
tokenType = "Bearer";
}

String scope = accessToken.getScope();
if (scope == null) {
scope = (String) cfg.get("scopes");
Expand Down
3 changes: 3 additions & 0 deletions ios/OAuthManager/OAuthManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,9 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
NSDictionary *headers = [opts objectForKey:@"headers"];
if (headers != nil) {
NSMutableDictionary *existingHeaders = [request.HTTPHeaders mutableCopy];
if (existingHeaders == nil) {
existingHeaders = [@{} mutableCopy];
}
for (NSString *header in headers) {
[existingHeaders setValue:[headers valueForKey:header] forKey:header];
}
Expand Down