Repository navigation
Expand file tree
/
Copy pathTokens.java
More file actions
121 lines (107 loc) · 5.13 KB
/
Copy pathTokens.java
File metadata and controls
121 lines (107 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.iam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.ClientConfig;
import org.json.JSONException;
import org.json.JSONObject;
@Path("/tokens")
public class Tokens {
private static Client client;
static ClientBuilder clientBuilder = ClientBuilder.newBuilder();
static ClientConfig config = new ClientConfig();
public static final String USERNAME="iamadmin";
public static final String PASSWORD="iam@1234";
static String refresh_token = null;
static String clientTokenUser = "Basic SUFNdXNlcnM6aWFtQDEyMzQ=";
static String clientTokenDevice = "Basic SUFNZGV2aWNlczppYW1AMTIzNA==";
@GET
@Path("/sso")
@Produces("application/json")
public static Response getSSOToken() throws JSONException{
client = clientBuilder.withConfig(config).build();
WebTarget webTarget = client.target("http://openam.dev.project.net:8080/openam/json").path("/ManagedPeople").path("/authenticate");
Response response = webTarget.request().header("X-OpenAM-Username",USERNAME).header("X-OpenAM-Password", PASSWORD)
.header("Content-Type", "application/json").post(Entity.json(""));
String str = response.readEntity(String.class);
JSONObject jsonObj = null;
jsonObj = new JSONObject(str);
String tokenId = jsonObj.get("tokenId").toString();
return Response.status(200).entity(str).build();
//response.readEntity(SSOTokenResponse.class);
}
public static String getSSOTokenOnly() throws JSONException{
Response resp=getSSOToken();
String tokenJsonString = resp.getEntity().toString();
JSONObject tokenJson = new JSONObject(tokenJsonString);
String token = tokenJson.get("tokenId").toString();
return token;
}
@GET
@Path("/admin")
@Produces("application/json")
public static String getAdminSSOToken() throws JSONException{
client = clientBuilder.withConfig(config).build();
WebTarget webTarget = client.target("http://openam.dev.project.net:8080/openam/json").path("/authenticate");
Response response = webTarget.request().header("X-OpenAM-Username","amadmin").header("X-OpenAM-Password", "TycoNET12")
.header("Content-Type", "application/json").post(Entity.json(""));
String str = response.readEntity(String.class);
JSONObject jsonObj = null;
jsonObj = new JSONObject(str);
String tokenId = jsonObj.get("tokenId").toString();
System.out.println(tokenId);
return tokenId;
}
@GET
@Path("/other")
@Produces("application/json")
public static String getOtherUserSSOToken() throws JSONException{
client = clientBuilder.withConfig(config).build();
WebTarget webTarget = client.target("http://openam.dev.project.net:8080/openam/json").path("/authenticate");
Response response = webTarget.request().header("X-OpenAM-Username","user1").header("X-OpenAM-Password", "tyco@123")
.header("Content-Type", "application/json").post(Entity.json(""));
String str = response.readEntity(String.class);
JSONObject jsonObj = null;
jsonObj = new JSONObject(str);
String tokenId = jsonObj.get("tokenId").toString();
System.out.println(tokenId);
return tokenId;
}
@GET
@Path("/access")
@Produces("application/json")
public static Response getAccessToken() throws JSONException{
client = clientBuilder.withConfig(config).build();
WebTarget webTarget = client.target("http://openam.dev.project.net:8080/openam/oauth2/access_token").queryParam("grant_type", "password")
.queryParam("username", "IAMdevices")
.queryParam("password", "iam@1234")
.queryParam("scope", "cn")
.queryParam("realm", "/ManagedDevices");
Response response = webTarget.request().header("Authorization",clientTokenDevice).post(Entity.json(""));
String str = response.readEntity(String.class);
System.out.println(str);
JSONObject jsonObj = null;
jsonObj = new JSONObject(str);
String access_token = jsonObj.get("access_token").toString();
refresh_token = jsonObj.get("refresh_token").toString();
return Response.ok().entity(str).build();
}
//Get access token from refresh token
@GET
@Path("/refresh")
@Produces("application/json")
public static Response getAccessTokenFromRefreshToken() throws JSONException{
WebTarget webTarget = client.target("http://openam.dev.project.net:8080/openam/oauth2/access_token").queryParam("grant_type", "refresh_token")
.queryParam("refresh_token", refresh_token)
.queryParam("realm", "/ManagedDevices");
Response response = webTarget.request().header("Authorization",clientTokenDevice).post(Entity.json(""));
//System.out.println(refresh_token);
String str = response.readEntity(String.class);
return Response.ok().entity(str).build();
}
}