Search in sources :

Example 1 with AuthenticationVO

use of org.olat.restapi.support.vo.AuthenticationVO in project OpenOLAT by OpenOLAT.

the class UserAuthenticationMgmtTest method createAuthentications.

@Test
public void createAuthentications() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    Identity adminIdent = securityManager.findIdentityByName("administrator");
    try {
        Authentication refAuth = securityManager.findAuthentication(adminIdent, "REST-API");
        if (refAuth != null) {
            securityManager.deleteAuthentication(refAuth);
        }
    } catch (Exception e) {
    // 
    }
    DBFactory.getInstance().commitAndCloseSession();
    assertTrue(conn.login("administrator", "openolat"));
    AuthenticationVO vo = new AuthenticationVO();
    vo.setAuthUsername("administrator");
    vo.setIdentityKey(adminIdent.getKey());
    vo.setProvider("REST-API");
    vo.setCredential("credentials");
    URI request = UriBuilder.fromUri(getContextURI()).path("/users/administrator/auth").build();
    HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true);
    conn.addJsonEntity(method, vo);
    HttpResponse response = conn.execute(method);
    assertTrue(response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 201);
    AuthenticationVO savedAuth = conn.parse(response, AuthenticationVO.class);
    Authentication refAuth = securityManager.findAuthentication(adminIdent, "REST-API");
    assertNotNull(refAuth);
    assertNotNull(refAuth.getKey());
    assertTrue(refAuth.getKey().longValue() > 0);
    assertNotNull(savedAuth);
    assertNotNull(savedAuth.getKey());
    assertTrue(savedAuth.getKey().longValue() > 0);
    assertEquals(refAuth.getKey(), savedAuth.getKey());
    assertEquals(refAuth.getAuthusername(), savedAuth.getAuthUsername());
    assertEquals(refAuth.getIdentity().getKey(), savedAuth.getIdentityKey());
    assertEquals(refAuth.getProvider(), savedAuth.getProvider());
    assertEquals(refAuth.getCredential(), savedAuth.getCredential());
    conn.shutdown();
}
Also used : Authentication(org.olat.basesecurity.Authentication) HttpResponse(org.apache.http.HttpResponse) Identity(org.olat.core.id.Identity) AuthenticationVO(org.olat.restapi.support.vo.AuthenticationVO) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 2 with AuthenticationVO

use of org.olat.restapi.support.vo.AuthenticationVO in project OpenOLAT by OpenOLAT.

the class UserAuthenticationMgmtTest method createAuthentications_checkDuplicate.

/**
 * Check if the REST call return a specific error if the pair authentication user name and provider
 * is already used.
 */
@Test
public void createAuthentications_checkDuplicate() throws IOException, URISyntaxException {
    Identity id1 = JunitTestHelper.createAndPersistIdentityAsRndUser("check-auth-1");
    Identity id2 = JunitTestHelper.createAndPersistIdentityAsRndUser("check-auth-2");
    String authUsername = UUID.randomUUID().toString();
    dbInstance.commitAndCloseSession();
    RestConnection conn = new RestConnection();
    Assert.assertTrue(conn.login("administrator", "openolat"));
    // set the first authentication
    AuthenticationVO vo1 = new AuthenticationVO();
    vo1.setAuthUsername(authUsername);
    vo1.setIdentityKey(id1.getKey());
    vo1.setProvider("REST-API");
    vo1.setCredential("credentials");
    URI request1 = UriBuilder.fromUri(getContextURI()).path("/users/" + id1.getName() + "/auth").build();
    HttpPut method1 = conn.createPut(request1, MediaType.APPLICATION_JSON, true);
    conn.addJsonEntity(method1, vo1);
    HttpResponse response1 = conn.execute(method1);
    Assert.assertEquals(200, response1.getStatusLine().getStatusCode());
    conn.parse(response1, AuthenticationVO.class);
    Authentication refAuth1 = securityManager.findAuthentication(id1, "REST-API");
    Assert.assertNotNull(refAuth1);
    Assert.assertEquals(id1, refAuth1.getIdentity());
    // set the second which duplicates the first
    AuthenticationVO vo2 = new AuthenticationVO();
    vo2.setAuthUsername(authUsername);
    vo2.setIdentityKey(id2.getKey());
    vo2.setProvider("REST-API");
    vo2.setCredential("credentials");
    URI request2 = UriBuilder.fromUri(getContextURI()).path("/users/" + id2.getName() + "/auth").build();
    HttpPut method2 = conn.createPut(request2, MediaType.APPLICATION_JSON, true);
    conn.addJsonEntity(method2, vo2);
    HttpResponse response2 = conn.execute(method2);
    Assert.assertEquals(409, response2.getStatusLine().getStatusCode());
    ErrorVO error = conn.parse(response2, ErrorVO.class);
    Assert.assertNotNull(error);
    conn.shutdown();
}
Also used : ErrorVO(org.olat.restapi.support.vo.ErrorVO) Authentication(org.olat.basesecurity.Authentication) HttpResponse(org.apache.http.HttpResponse) Identity(org.olat.core.id.Identity) AuthenticationVO(org.olat.restapi.support.vo.AuthenticationVO) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 3 with AuthenticationVO

use of org.olat.restapi.support.vo.AuthenticationVO in project openolat by klemens.

the class UserAuthenticationMgmtTest method getAuthentications.

@Test
public void getAuthentications() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    assertTrue(conn.login("administrator", "openolat"));
    URI request = UriBuilder.fromUri(getContextURI()).path("/users/administrator/auth").build();
    HttpGet method = conn.createGet(request, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    InputStream body = response.getEntity().getContent();
    List<AuthenticationVO> vos = parseAuthenticationArray(body);
    assertNotNull(vos);
    assertFalse(vos.isEmpty());
    conn.shutdown();
}
Also used : InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) AuthenticationVO(org.olat.restapi.support.vo.AuthenticationVO) URI(java.net.URI) Test(org.junit.Test)

Example 4 with AuthenticationVO

use of org.olat.restapi.support.vo.AuthenticationVO in project openolat by klemens.

the class UserAuthenticationMgmtTest method createAuthentications.

@Test
public void createAuthentications() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    Identity adminIdent = securityManager.findIdentityByName("administrator");
    try {
        Authentication refAuth = securityManager.findAuthentication(adminIdent, "REST-API");
        if (refAuth != null) {
            securityManager.deleteAuthentication(refAuth);
        }
    } catch (Exception e) {
    // 
    }
    DBFactory.getInstance().commitAndCloseSession();
    assertTrue(conn.login("administrator", "openolat"));
    AuthenticationVO vo = new AuthenticationVO();
    vo.setAuthUsername("administrator");
    vo.setIdentityKey(adminIdent.getKey());
    vo.setProvider("REST-API");
    vo.setCredential("credentials");
    URI request = UriBuilder.fromUri(getContextURI()).path("/users/administrator/auth").build();
    HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true);
    conn.addJsonEntity(method, vo);
    HttpResponse response = conn.execute(method);
    assertTrue(response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 201);
    AuthenticationVO savedAuth = conn.parse(response, AuthenticationVO.class);
    Authentication refAuth = securityManager.findAuthentication(adminIdent, "REST-API");
    assertNotNull(refAuth);
    assertNotNull(refAuth.getKey());
    assertTrue(refAuth.getKey().longValue() > 0);
    assertNotNull(savedAuth);
    assertNotNull(savedAuth.getKey());
    assertTrue(savedAuth.getKey().longValue() > 0);
    assertEquals(refAuth.getKey(), savedAuth.getKey());
    assertEquals(refAuth.getAuthusername(), savedAuth.getAuthUsername());
    assertEquals(refAuth.getIdentity().getKey(), savedAuth.getIdentityKey());
    assertEquals(refAuth.getProvider(), savedAuth.getProvider());
    assertEquals(refAuth.getCredential(), savedAuth.getCredential());
    conn.shutdown();
}
Also used : Authentication(org.olat.basesecurity.Authentication) HttpResponse(org.apache.http.HttpResponse) Identity(org.olat.core.id.Identity) AuthenticationVO(org.olat.restapi.support.vo.AuthenticationVO) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 5 with AuthenticationVO

use of org.olat.restapi.support.vo.AuthenticationVO in project openolat by klemens.

the class UserAuthenticationWebService method getAuthenticationTokenList.

/**
 * Returns all user authentications
 * @response.representation.200.qname {http://www.example.com}authenticationVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The list of all users in the OLAT system
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_AUTHVOes}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @response.representation.404.doc The identity not found
 * @param username The username of the user to retrieve authentication
 * @param request The HTTP request
 * @return
 */
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getAuthenticationTokenList(@PathParam("username") String username, @Context HttpServletRequest request) {
    if (!isUserManager(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    BaseSecurity baseSecurity = BaseSecurityManager.getInstance();
    Identity identity = baseSecurity.findIdentityByName(username);
    if (identity == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    List<Authentication> authentications = baseSecurity.getAuthentications(identity);
    AuthenticationVO[] vos = new AuthenticationVO[authentications.size()];
    int count = 0;
    for (Authentication authentication : authentications) {
        vos[count++] = ObjectFactory.get(authentication, false);
    }
    return Response.ok(vos).build();
}
Also used : Authentication(org.olat.basesecurity.Authentication) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) Identity(org.olat.core.id.Identity) AuthenticationVO(org.olat.restapi.support.vo.AuthenticationVO) BaseSecurity(org.olat.basesecurity.BaseSecurity) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

AuthenticationVO (org.olat.restapi.support.vo.AuthenticationVO)12 Authentication (org.olat.basesecurity.Authentication)8 Identity (org.olat.core.id.Identity)8 URI (java.net.URI)6 HttpResponse (org.apache.http.HttpResponse)6 Test (org.junit.Test)6 Produces (javax.ws.rs.Produces)4 HttpPut (org.apache.http.client.methods.HttpPut)4 BaseSecurity (org.olat.basesecurity.BaseSecurity)4 RestSecurityHelper.getIdentity (org.olat.restapi.security.RestSecurityHelper.getIdentity)4 ErrorVO (org.olat.restapi.support.vo.ErrorVO)4 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 URISyntaxException (java.net.URISyntaxException)2 Consumes (javax.ws.rs.Consumes)2 GET (javax.ws.rs.GET)2 PUT (javax.ws.rs.PUT)2 HttpGet (org.apache.http.client.methods.HttpGet)2