Search in sources :

Example 11 with Scope

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;
}
Also used : Scope(org.oxauth.persistence.model.Scope)

Example 12 with 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);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) Scope(org.oxauth.persistence.model.Scope) HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HttpResponse(org.apache.http.HttpResponse) ParseException(org.apache.http.ParseException) IOException(java.io.IOException) Test(org.junit.Test)

Example 13 with Scope

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);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntity(org.apache.http.HttpEntity) Scope(org.oxauth.persistence.model.Scope) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) ParseException(org.apache.http.ParseException) IOException(java.io.IOException) Test(org.junit.Test)

Example 14 with Scope

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);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) Scope(org.oxauth.persistence.model.Scope) HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HttpResponse(org.apache.http.HttpResponse) ParseException(org.apache.http.ParseException) IOException(java.io.IOException) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 15 with Scope

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);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) Scope(org.oxauth.persistence.model.Scope) HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HttpResponse(org.apache.http.HttpResponse) ParseException(org.apache.http.ParseException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

Scope (org.oxauth.persistence.model.Scope)63 ArrayList (java.util.ArrayList)15 IOException (java.io.IOException)12 Operation (io.swagger.v3.oas.annotations.Operation)10 ProtectedApi (org.gluu.oxtrust.service.filter.ProtectedApi)10 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)9 HttpEntity (org.apache.http.HttpEntity)8 HttpResponse (org.apache.http.HttpResponse)8 ParseException (org.apache.http.ParseException)8 GluuAttribute (org.gluu.model.GluuAttribute)8 Test (org.junit.Test)8 User (org.gluu.oxauth.model.common.User)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 Test (org.testng.annotations.Test)7 BasePersistenceException (org.gluu.persist.exception.BasePersistenceException)5 HttpGet (org.apache.http.client.methods.HttpGet)4 HttpPost (org.apache.http.client.methods.HttpPost)4 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)4 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)4 Filter (org.gluu.search.filter.Filter)4