-
Notifications
You must be signed in to change notification settings - Fork 36
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
Sp provision api feature #357
base: sp-provision-api-feature
Are you sure you want to change the base?
Changes from 7 commits
bfa9205
d777b72
2e1bb56
6e88f43
f61296c
581316c
1dcea92
7308cb8
49987bb
5cfdb5e
2528aeb
9a4fb70
e51a74e
55ce98f
dac1a53
215e150
95491e9
b64bb1c
9bb78e7
d72aa01
42f204e
7004625
48cdc52
c18fc79
2780cfb
ff79163
ced4a5b
5437ca7
626e596
aa11d18
f09c440
2307d03
7266c4f
9091a05
3632c46
049b087
8e6f587
18de31d
2c8528b
6c78d57
a63e610
5a1a1f0
f28f2d6
f20f1db
2111664
9cc47a0
dc65d97
1b95a62
ce3f870
aae9b28
6b90680
012421e
3f347c1
4e021b5
fb33566
0086b31
ce1ef6a
920f233
96ca309
ae8d049
56d113b
1fc8120
1146231
76d204c
3a9d099
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- Copyright (c) 2015-2016, WSO2.Telco Inc. (http://www.wso2telco.com) | ||
All Rights Reserved. WSO2.Telco Inc. licences this file to you 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. --> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<parent> | ||
<groupId>com.wso2telco.ids</groupId> | ||
<artifactId>component-ids</artifactId> | ||
<version>2.4.0-SNAPSHOT</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>sp-provision-service</artifactId> | ||
<packaging>war</packaging> | ||
<name>Service Provider Provisioning Service</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.httpcomponents</groupId> | ||
<artifactId>httpcore</artifactId> | ||
<version>4.4.5</version> | ||
<type>jar</type> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.vaadin.external.google</groupId> | ||
<artifactId>android-json</artifactId> | ||
<version>0.0.20131108.vaadin1</version> | ||
<type>jar</type> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>3.8.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.httpcomponents</groupId> | ||
<artifactId>httpclient</artifactId> | ||
<version>4.5.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>net.iharder</groupId> | ||
<artifactId>base64</artifactId> | ||
<version>2.3.8</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.wso2.carbon.identity</groupId> | ||
<artifactId>org.wso2.carbon.identity.oauth.stub</artifactId> | ||
<version>5.1.3</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>log4j</groupId> | ||
<artifactId>log4j</artifactId> | ||
<version>1.2.17</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.wso2telco.core</groupId> | ||
<artifactId>config-util</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.wso2.carbon.identity</groupId> | ||
<artifactId>org.wso2.carbon.identity.application.mgt.stub</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.code.gson</groupId> | ||
<artifactId>gson</artifactId> | ||
<version>2.8.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.squareup.okhttp3</groupId> | ||
<artifactId>okhttp</artifactId> | ||
<version>3.10.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.wso2telco.core</groupId> | ||
<artifactId>dbutils</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<finalName>service-provider-provision</finalName> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>${maven-compiler-plugin.version}</source> | ||
<target>${maven-compiler-plugin.version}</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-war-plugin</artifactId> | ||
<version>${maven-war-plugin.version}</version> | ||
<configuration> | ||
<webResources> | ||
<resource> | ||
<!-- this is relative to the pom.xml directory --> | ||
<directory>src/main/webapp</directory> | ||
</resource> | ||
</webResources> | ||
<warName>service-provider-provision</warName> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>7</source> | ||
<target>7</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
/** | ||
* **************************************************************************** | ||
* Copyright (c) 2015-2018, WSO2.Telco Inc. (http://www.wso2telco.com) All Rights Reserved. | ||
* <p> | ||
* WSO2.Telco Inc. licences this file to you 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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 com.wso2telco.spprovisionapp.api; | ||
|
||
import com.google.gson.Gson; | ||
import com.wso2telco.core.config.model.MobileConnectConfig; | ||
import com.wso2telco.core.config.service.ConfigurationService; | ||
import com.wso2telco.core.config.service.ConfigurationServiceImpl; | ||
import com.wso2telco.spprovisionapp.exception.HttpResponseIsEmptyException; | ||
import com.wso2telco.spprovisionapp.model.TokenResponse; | ||
import com.wso2telco.spprovisionapp.utils.HttpClientUtil; | ||
import net.iharder.Base64; | ||
import okhttp3.*; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.HttpVersion; | ||
import org.apache.http.client.CookieStore; | ||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.conn.ClientConnectionManager; | ||
import org.apache.http.conn.scheme.PlainSocketFactory; | ||
import org.apache.http.conn.scheme.Scheme; | ||
import org.apache.http.conn.scheme.SchemeRegistry; | ||
import org.apache.http.conn.ssl.SSLSocketFactory; | ||
import org.apache.http.cookie.Cookie; | ||
import org.apache.http.impl.client.DefaultHttpClient; | ||
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; | ||
import org.apache.http.params.BasicHttpParams; | ||
import org.apache.http.params.HttpParams; | ||
import org.apache.http.params.HttpProtocolParams; | ||
import org.apache.http.protocol.HTTP; | ||
import org.apache.log4j.Logger; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.URLEncoder; | ||
import java.security.KeyStore; | ||
import java.util.List; | ||
|
||
public class ApiCallsInAM { | ||
static final Logger logInstance = Logger.getLogger(ApiCallsInAM.class); | ||
|
||
private String amUserCreationAPI, amLoginAPI, amAppCreationAPI, addSubscriptionAPI, password, appName, tokenUrlAm, | ||
tokenEndpoint; | ||
private List<Cookie> cookies; | ||
private CookieStore cookieStore; | ||
private static ConfigurationService configurationService = new ConfigurationServiceImpl(); | ||
private static MobileConnectConfig mobileConnectConfigs; | ||
|
||
static { | ||
mobileConnectConfigs = configurationService.getDataHolder().getMobileConnectConfig(); | ||
} | ||
|
||
|
||
public ApiCallsInAM() { | ||
String host; | ||
host = mobileConnectConfigs.getSpProvisionConfig().getApiManagerUrl(); | ||
tokenUrlAm = mobileConnectConfigs.getSpProvisionConfig().getAmTokenUrl(); | ||
tokenEndpoint = mobileConnectConfigs.getSpProvisionConfig().getTokenUrl(); | ||
|
||
amUserCreationAPI = host + "/store/site/blocks/user/sign-up/ajax/user-add.jag"; | ||
amLoginAPI = host + "/store/site/blocks/user/login/ajax/login.jag"; | ||
amAppCreationAPI = host + "/store/site/blocks/application/application-add/ajax/application-add.jag"; | ||
addSubscriptionAPI = host + "/store/site/blocks/subscription/subscription-add/ajax/subscription-add.jag"; | ||
password = mobileConnectConfigs.getSpProvisionConfig().getDefaultUserPassword(); | ||
} | ||
|
||
private static String postToURLjson(String url, HttpClient httpClient) { | ||
try { | ||
HttpPost postRequest = new HttpPost(url); | ||
|
||
postRequest.addHeader("Content-Type", "application/json"); | ||
HttpResponse response = httpClient.execute(postRequest); | ||
|
||
BufferedReader br = new BufferedReader( | ||
new InputStreamReader((response.getEntity().getContent()))); | ||
|
||
String output; | ||
StringBuffer totalOutput = new StringBuffer(); | ||
String postResponse; | ||
|
||
while ((output = br.readLine()) != null) { | ||
totalOutput.append(output); | ||
} | ||
|
||
postResponse = "{\"httpCode\": " + response.getStatusLine().getStatusCode() + ",\"output\": " | ||
+ totalOutput.toString() + "}"; | ||
return postResponse; | ||
} catch (IOException ex) { | ||
logInstance.error("IOException occured in reading responce from AM APIs:" + ex.toString(), ex); | ||
String postResponse = "{\"exception\": " + ex.toString() + "\"}"; | ||
return postResponse; | ||
} | ||
} | ||
|
||
/* | ||
* Create new User in AM | ||
*/ | ||
public String createNewUserInAm(String appName, String firstName, String lastName, String devEmail) throws | ||
IOException { | ||
|
||
String url = amUserCreationAPI + "?allFieldsValues=" + firstName + "%7C" + lastName + "%7C" + devEmail | ||
+ "&username=" + appName + "&password=" + password + "&action=addUser"; | ||
HttpClient httpClient = getNewHttpClient(); | ||
String response = postToURLjson(url, httpClient); | ||
httpClient.getConnectionManager().shutdown(); | ||
return response; | ||
} | ||
|
||
/* | ||
* Login the new User to API | ||
*/ | ||
public String loginToAm(String appName) throws IOException { | ||
cookieStore = null; | ||
|
||
|
||
String url = amLoginAPI + "?username=" + appName + "&password=" | ||
+ URLEncoder.encode(password, "UTF-8") + "&action=login"; | ||
DefaultHttpClient httpClient = getNewHttpClient(); | ||
String response = postToURLjson(url, httpClient); | ||
httpClient.getConnectionManager().shutdown(); | ||
cookieStore = httpClient.getCookieStore(); | ||
return response; | ||
} | ||
|
||
/* | ||
* add subscriptions to the newly created app | ||
*/ | ||
/* | ||
* Service provider get scope | ||
*/ | ||
|
||
/* | ||
* Create application in AM | ||
*/ | ||
public String createApplication(String appName, String description, String callback, String tier) | ||
throws IOException { | ||
|
||
String url = amAppCreationAPI + "?action=addApplication&application=" + appName + "&tier=" + tier | ||
+ "&description=" + description + "&callbackUrl=" + callback + ""; | ||
DefaultHttpClient httpClient = getNewHttpClient(); | ||
httpClient.setCookieStore(cookieStore); | ||
String response = postToURLjson(url, httpClient); | ||
httpClient.getConnectionManager().shutdown(); | ||
return response; | ||
} | ||
|
||
public String addSubscritions(String userName, String appName, String apiName, String apiVersion, | ||
String apiProvider, String tier) { | ||
|
||
try { | ||
loginToAm(userName); | ||
} catch (IOException ex) { | ||
logInstance.error("IO Exception occured when trying to login to the user in Add subscription " + | ||
"process" + ex.toString(), ex); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. llogInstance.error("IO Exception occured when trying to login to the user in Add subscription " +
|
||
} | ||
String url = addSubscriptionAPI + "?action=addAPISubscription&name=" + apiName + "&version=" + apiVersion | ||
+ "&provider=" + apiProvider + "&tier=" + tier + "&applicationName=" + appName + ""; | ||
DefaultHttpClient httpClient = getNewHttpClient(); | ||
httpClient.setCookieStore(cookieStore); | ||
String response = postToURLjson(url, httpClient); | ||
httpClient.getConnectionManager().shutdown(); | ||
return response; | ||
} | ||
|
||
public String getAccessToken(String consumerKey, String consumerSecret) throws IOException { | ||
|
||
return getTokenWithOkHttpClient(consumerKey, consumerSecret); | ||
} | ||
|
||
private String getTokenWithOkHttpClient(String consumerKey, String consumerSecret) throws IOException { | ||
OkHttpClient client = HttpClientUtil.getUnsafeOkHttpClient();//HttpClientUtil.getUnsecureHttpClient();//null;//new OkHttpClient(); | ||
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); | ||
RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials"); | ||
String encoding = Base64.encodeBytes((consumerKey + ":" + consumerSecret).getBytes()); | ||
Request request = new Request.Builder() | ||
.url(tokenEndpoint) | ||
.post(body) | ||
.addHeader("authorization", "Basic " + encoding) | ||
.addHeader("content-type", "application/x-www-form-urlencoded") | ||
.addHeader("cache-control", "no-cache") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove |
||
.addHeader("postman-token", "37e6d409-ba6d-b94d-50d2-273b7d671a84") | ||
.build(); | ||
|
||
Response response = client.newCall(request).execute(); | ||
if (response != null && response.body() != null) { | ||
TokenResponse tokenResponse = new Gson().fromJson(response.body().string(), TokenResponse.class); | ||
|
||
return tokenResponse.getAccessToken(); | ||
} else { | ||
throw new HttpResponseIsEmptyException("Response is empty"); | ||
} | ||
} | ||
|
||
|
||
public DefaultHttpClient getNewHttpClient() { | ||
try { | ||
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); | ||
trustStore.load(null, null); | ||
|
||
SslSocketFactory sf = new SslSocketFactory(trustStore); | ||
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); | ||
|
||
HttpParams parameters = new BasicHttpParams(); | ||
HttpProtocolParams.setVersion(parameters, HttpVersion.HTTP_1_1); | ||
HttpProtocolParams.setContentCharset(parameters, HTTP.UTF_8); | ||
|
||
SchemeRegistry registry = new SchemeRegistry(); | ||
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); | ||
registry.register(new Scheme("https", sf, 443)); | ||
|
||
ClientConnectionManager ccm = new ThreadSafeClientConnManager(parameters, | ||
registry); | ||
|
||
return new DefaultHttpClient(ccm, parameters); | ||
} catch (Exception exception) { | ||
logInstance.error("Exception" + exception.getMessage(), exception); | ||
return new DefaultHttpClient(); | ||
} | ||
} | ||
|
||
private String convertStringToJson(String value) throws JSONException { | ||
JSONObject jObject = new JSONObject(value); | ||
String accessToken = jObject.getString("access_token"); | ||
return accessToken; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use correct year