Skip to content

Commit

Permalink
Merge pull request #64 from seantdg/issue63
Browse files Browse the repository at this point in the history
Issue 63 Fix - Allow bearer token to be passed as a parameter
  • Loading branch information
Madhan Sadasivam authored Feb 6, 2017
2 parents cb053f5 + bbec04b commit a6b2beb
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,16 @@ If the API takes a long time to package up then it is likely that the token til

Depending on where the plugin is in the order, and how much validation is requird, it is possible that this may still result in token timeout.

### Passing the Bearer Token as a parameter
If you would like to generate the bearer token outside of this plugin and provide it as a command line parameter, you can add the following:

<apigee.bearer>${bearer}</apigee.bearer>

Provide the token when invoking the plugin.

mvn install -Ptest -Dusername=$ae_username -Dpassword=$ae_password \
-Dorg=testmyapi -Dauthtype=oauth -Dbearer=c912eu1201c

## Deploying API Proxies with Node.js apps

Starting at version 1.0.1 of the plugin, support for API proxies that contain node.js applications is included. The plugin
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@
<name>Mike Dunker</name>
</developer>

<developer>
<id>seantdg</id>
<name>Sean 'Handsome' Davis</name>
</developer>




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
<apigee.tokenurl>${tokenurl}</apigee.tokenurl> <!-- optional: oauth -->
<apigee.mfatoken>${mfatoken}</apigee.mfatoken> <!-- optional: mfa -->
<apigee.authtype>${authtype}</apigee.authtype> <!-- optional: oauth|basic(default) -->
<apigee.bearer>${bearer}</apigee.bearer> <!-- optional: Bearer token override -->
<!--apigee.override.delay>10</apigee.override.delay-->
<!--apigee.delay>1000</apigee.delay-->
</properties>
Expand All @@ -133,6 +134,7 @@
<apigee.tokenurl>${tokenurl}</apigee.tokenurl> <!-- optional: oauth -->
<apigee.mfatoken>${mfatoken}</apigee.mfatoken> <!-- optional: mfa -->
<apigee.authtype>${authtype}</apigee.authtype> <!-- optional: oauth|basic(default) -->
<apigee.bearer>${bearer}</apigee.bearer> <!-- optional: Bearer token override -->
<!--apigee.override.delay>10</apigee.override.delay-->
<!--apigee.delay>1000</apigee.delay-->
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ public abstract class GatewayAbstractMojo extends AbstractMojo {
*/
private String orgName;

/**
* Gateway host bearer token
*
* @parameter expression="${apigee.bearer}"
*/
private String bearer;

/**
* Gateway host username
*
Expand Down Expand Up @@ -204,6 +211,7 @@ public ServerProfile getProfile() {
this.buildProfile.setMFAToken(this.mfaToken);
this.buildProfile.setAuthType(this.authType);
this.buildProfile.setEnvironment(this.deploymentEnv);
this.buildProfile.setBearerToken(this.bearer);
this.buildProfile.setCredential_user(this.userName);
this.buildProfile.setCredential_pwd(this.password);
this.buildProfile.setProfileId(this.id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,11 @@ private static HttpResponse executeAPI(ServerProfile profile, HttpRequest reques
}

/**** OAuth ****/
if (accessToken != null) {
if (profile.getBearerToken() != null) {
accessToken = profile.getBearerToken();
headers.setAuthorization("Bearer " + accessToken);
}
else if (accessToken != null) {
// subsequent calls
logger.debug("Reusing mgmt API access token");
headers.setAuthorization("Bearer " + accessToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ServerProfile {
// https://api.enterprise.apigee.com
private String tokenURL; // Mgmt API OAuth token endpoint
private String mfaToken; // Mgmt API OAuth MFA - TOTP
private String bearerToken; //Mgmt API OAuth Token
private String authType; // Mgmt API Auth Type oauth|basic
private String environment; // prod or test
private String api_version; // v2 or v1 in the server url
Expand Down Expand Up @@ -92,6 +93,14 @@ public void setMFAToken(String otp) {
this.mfaToken = otp;
}

public String getBearerToken() {
return this.bearerToken;
}

public void setBearerToken(String token) {
this.bearerToken = token;
}

public String getApi_type() {
return api_type;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright (C) 2016 Apigee Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.apigee.buildTools.enterprise4g.test;

import io.apigee.buildTools.enterprise4g.rest.RestUtil;
import io.apigee.buildTools.enterprise4g.utils.ServerProfile;
import com.apigee.mgmtapi.sdk.client.MgmtAPIClient;
import com.apigee.mgmtapi.sdk.model.AccessToken;
import junit.framework.TestCase;

import java.io.IOException;

public class TestGetRevisionWithBearer extends TestCase {

ServerProfile profile = new ServerProfile();

protected void setUp() throws Exception {
super.setUp();

profile.setHostUrl("https://api.enterprise.apigee.com");
profile.setApplication("taskservice");
profile.setCredential_user(System.getProperty("username"));
profile.setCredential_pwd(System.getProperty("password"));
profile.setEnvironment(System.getProperty("env"));
profile.setOrg(System.getProperty("org"));
profile.setApi_version("v1");
profile.setBearerToken(generateAccessToken());
profile.setTokenUrl("https://login.apigee.com/oauth/token");

}

public void testGetRevisionCall() throws IOException{
RestUtil.getRevision(profile);
System.out.println("revision number::"+ RestUtil.getVersionRevision());
assertNotNull(RestUtil.getVersionRevision());
}

public void testGetLatestRevisionCall() throws IOException{
String latestRev = RestUtil.getLatestRevision(profile);
assertNotNull(latestRev);
}

//the client can generate the token using any other plugin they choose
private String generateAccessToken() throws Exception{
MgmtAPIClient client = new MgmtAPIClient();
AccessToken token = client.getAccessToken(
"https://login.apigee.com/oauth/token",
"edgecli", "edgeclisecret",
System.getProperty("username"),
System.getProperty("password"));
return token.getAccess_token();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static Test suite() {
suite.addTestSuite(TestGetDeployedRevision.class);
suite.addTestSuite(TestRefreshBundle.class);
suite.addTestSuite(TestDeleteDeployedBundle.class);
suite.addTestSuite(TestGetRevisionWithBearer.class);
//$JUnit-END$
return suite;
}
Expand Down

0 comments on commit a6b2beb

Please sign in to comment.