use of org.oxauth.persistence.model.Scope in project oxAuth by GluuFederation.
the class SpontaneousScopeService method createSpontaneousScopeIfNeeded.
public Scope createSpontaneousScopeIfNeeded(Set<String> regExps, String scopeId, String clientId) {
Scope fromPersistence = scopeService.getScopeById(scopeId);
if (fromPersistence != null) {
// scope already exists
return fromPersistence;
}
final Pair<Boolean, String> isAllowed = isAllowedBySpontaneousScopes(regExps, scopeId);
if (!isAllowed.getFirst()) {
log.error("Forbidden by client. Check client configuration.");
return null;
}
Scope regexpScope = scopeService.getScopeById(isAllowed.getSecond());
Scope scope = new Scope();
scope.setDefaultScope(false);
scope.setDescription("Spontaneous scope: " + scope);
scope.setDisplayName(scopeId);
scope.setId(scopeId);
scope.setInum(UUID.randomUUID().toString());
scope.setScopeType(ScopeType.SPONTANEOUS);
scope.setDeletable(true);
scope.setExpirationDate(new Date(getLifetime()));
scope.setDn("inum=" + scope.getInum() + "," + staticConfiguration.getBaseDn().getScopes());
scope.getAttributes().setSpontaneousClientId(clientId);
scope.getAttributes().setSpontaneousClientScopes(Lists.newArrayList(isAllowed.getSecond()));
scope.setUmaAuthorizationPolicies(regexpScope != null ? regexpScope.getUmaAuthorizationPolicies() : new ArrayList<>());
scopeService.persist(scope);
log.trace("Created spontaneous scope: " + scope.getId() + ", dn: " + scope.getDn());
return scope;
}
use of org.oxauth.persistence.model.Scope in project oxTrust by GluuFederation.
the class ScopeWebResourceTest method createScopeTest.
@Test
public void createScopeTest() {
String name = "ApiScope";
Scope scope = getScope(name);
HttpPost request = new HttpPost(BASE_URL + ApiConstants.BASE_API_URL + ApiConstants.SCOPES);
try {
String json = mapper.writeValueAsString(scope);
HttpEntity entity = new ByteArrayEntity(json.toString().getBytes("UTF-8"), ContentType.APPLICATION_FORM_URLENCODED);
request.setEntity(entity);
String CONTENT_TYPE = "Content-Type";
request.setHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON);
HttpResponse response = handle(request);
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
HttpEntity result = response.getEntity();
Scope myScope = mapper.readValue(EntityUtils.toString(result), Scope.class);
Assert.assertEquals(myScope.getDisplayName(), name);
Assert.assertEquals(myScope.getScopeType(), ScopeType.OPENID);
} catch (ParseException | IOException e) {
e.printStackTrace();
Assert.assertTrue(false);
}
}
use of org.oxauth.persistence.model.Scope in project oxTrust by GluuFederation.
the class ScopeWebResourceTest method getScopeByInumTest.
@Test
public void getScopeByInumTest() {
String inum = "F0C4";
HttpUriRequest request = new HttpGet(BASE_URL + ApiConstants.BASE_API_URL + ApiConstants.SCOPES + "/" + inum);
HttpResponse response = handle(request);
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
HttpEntity entity = response.getEntity();
try {
String content = EntityUtils.toString(entity);
Scope scope = mapper.readValue(content, Scope.class);
Assert.assertNotNull(scope);
} catch (ParseException | IOException e) {
e.printStackTrace();
Assert.assertTrue(false);
}
}
use of org.oxauth.persistence.model.Scope in project oxTrust by GluuFederation.
the class ScopeWebResourceTest method updateScopeTest.
@Test
public void updateScopeTest() {
String name = "ApiScopeUpdate";
Scope scope = getScope(name);
HttpPost request = new HttpPost(BASE_URL + ApiConstants.BASE_API_URL + ApiConstants.SCOPES);
try {
String json = mapper.writeValueAsString(scope);
HttpEntity entity = new ByteArrayEntity(json.toString().getBytes("UTF-8"), ContentType.APPLICATION_FORM_URLENCODED);
request.setEntity(entity);
String CONTENT_TYPE = "Content-Type";
request.setHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON);
HttpResponse response = handle(request);
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
HttpEntity result = response.getEntity();
Scope myScope = mapper.readValue(EntityUtils.toString(result), Scope.class);
Assert.assertEquals(myScope.getDisplayName(), name);
myScope.setDisplayName(myScope.getDisplayName() + " Updated");
HttpPut second = new HttpPut(BASE_URL + ApiConstants.BASE_API_URL + ApiConstants.SCOPES);
json = mapper.writeValueAsString(myScope);
entity = new ByteArrayEntity(json.toString().getBytes("UTF-8"), ContentType.APPLICATION_FORM_URLENCODED);
second.setEntity(entity);
second.setHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON);
response = handle(second);
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
} catch (ParseException | IOException e) {
e.printStackTrace();
Assert.assertTrue(false);
}
}
use of org.oxauth.persistence.model.Scope in project oxTrust by GluuFederation.
the class UmaScopeWebResourceTest method createUmaScopeTest.
@Test
public void createUmaScopeTest() {
String name = "ApiUmaScope";
Scope scope = getUmaScope(name);
HttpPost request = new HttpPost(BASE_URL + ApiConstants.BASE_API_URL + ApiConstants.UMA + ApiConstants.SCOPES);
try {
String json = mapper.writeValueAsString(scope);
HttpEntity entity = new ByteArrayEntity(json.toString().getBytes("UTF-8"), ContentType.APPLICATION_FORM_URLENCODED);
request.setEntity(entity);
String CONTENT_TYPE = "Content-Type";
request.setHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON);
HttpResponse response = handle(request);
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
HttpEntity result = response.getEntity();
Scope myScope = mapper.readValue(EntityUtils.toString(result), Scope.class);
Assert.assertNotNull(myScope);
Assert.assertEquals(myScope.getDisplayName(), name);
} catch (ParseException | IOException e) {
e.printStackTrace();
Assert.assertTrue(false);
}
}
Aggregations