Search in sources :

Example 6 with AuthenticationVO

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

the class UserAuthenticationWebService method create.

/**
 * Creates and persists an authentication
 * @response.representation.qname {http://www.example.com}authenticationVO
 * @response.representation.mediaType application/xml, application/json
 * @response.representation.doc An authentication to save
 * @response.representation.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_AUTHVO}
 * @response.representation.200.qname {http://www.example.com}authenticationVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The saved authentication
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_AUTHVO}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @response.representation.404.doc The identity not found
 * @response.representation.406.doc Cannot create the authentication for an unkown reason
 * @response.representation.409.doc Cannot create the authentication because the authentication username is already used by someone else within the same provider
 * @param username The username of the user
 * @param authenticationVO The authentication object to persist
 * @param request The HTTP request
 * @return the saved authentication
 */
@PUT
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response create(@PathParam("username") String username, AuthenticationVO authenticationVO, @Context HttpServletRequest request) {
    if (!RestSecurityHelper.isUserManager(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    BaseSecurity baseSecurity = BaseSecurityManager.getInstance();
    Identity identity = baseSecurity.loadIdentityByKey(authenticationVO.getIdentityKey(), false);
    if (identity == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    if (!identity.getName().equals(username)) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    String provider = authenticationVO.getProvider();
    String authUsername = authenticationVO.getAuthUsername();
    String credentials = authenticationVO.getCredential();
    Authentication currentAuthentication = baseSecurity.findAuthenticationByAuthusername(authUsername, provider);
    if (currentAuthentication != null) {
        if (!currentAuthentication.getIdentity().equals(identity)) {
            ErrorVO error = new ErrorVO();
            error.setCode("unkown:409");
            error.setTranslation("Authentication name used by: " + currentAuthentication.getIdentity().getUser().getEmail());
            return Response.serverError().status(Status.CONFLICT).entity(error).build();
        }
    }
    Authentication authentication = baseSecurity.createAndPersistAuthentication(identity, provider, authUsername, credentials, null);
    if (authentication == null) {
        return Response.serverError().status(Status.NOT_ACCEPTABLE).build();
    }
    log.audit("New authentication created for " + authUsername + " with provider " + provider);
    AuthenticationVO savedAuth = ObjectFactory.get(authentication, true);
    return Response.ok(savedAuth).build();
}
Also used : ErrorVO(org.olat.restapi.support.vo.ErrorVO) 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) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 7 with AuthenticationVO

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

the class ObjectFactory method get.

public static AuthenticationVO get(Authentication authentication, boolean withCred) {
    AuthenticationVO vo = new AuthenticationVO();
    vo.setKey(authentication.getKey());
    vo.setIdentityKey(authentication.getIdentity().getKey());
    vo.setAuthUsername(authentication.getAuthusername());
    vo.setProvider(authentication.getProvider());
    if (withCred) {
        vo.setCredential(authentication.getCredential());
    }
    return vo;
}
Also used : AuthenticationVO(org.olat.restapi.support.vo.AuthenticationVO)

Example 8 with AuthenticationVO

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

the class UserAuthenticationWebService method create.

/**
 * Creates and persists an authentication
 * @response.representation.qname {http://www.example.com}authenticationVO
 * @response.representation.mediaType application/xml, application/json
 * @response.representation.doc An authentication to save
 * @response.representation.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_AUTHVO}
 * @response.representation.200.qname {http://www.example.com}authenticationVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The saved authentication
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_AUTHVO}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @response.representation.404.doc The identity not found
 * @response.representation.406.doc Cannot create the authentication for an unkown reason
 * @response.representation.409.doc Cannot create the authentication because the authentication username is already used by someone else within the same provider
 * @param username The username of the user
 * @param authenticationVO The authentication object to persist
 * @param request The HTTP request
 * @return the saved authentication
 */
@PUT
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response create(@PathParam("username") String username, AuthenticationVO authenticationVO, @Context HttpServletRequest request) {
    if (!RestSecurityHelper.isUserManager(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    BaseSecurity baseSecurity = BaseSecurityManager.getInstance();
    Identity identity = baseSecurity.loadIdentityByKey(authenticationVO.getIdentityKey(), false);
    if (identity == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    if (!identity.getName().equals(username)) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    String provider = authenticationVO.getProvider();
    String authUsername = authenticationVO.getAuthUsername();
    String credentials = authenticationVO.getCredential();
    Authentication currentAuthentication = baseSecurity.findAuthenticationByAuthusername(authUsername, provider);
    if (currentAuthentication != null) {
        if (!currentAuthentication.getIdentity().equals(identity)) {
            ErrorVO error = new ErrorVO();
            error.setCode("unkown:409");
            error.setTranslation("Authentication name used by: " + currentAuthentication.getIdentity().getUser().getEmail());
            return Response.serverError().status(Status.CONFLICT).entity(error).build();
        }
    }
    Authentication authentication = baseSecurity.createAndPersistAuthentication(identity, provider, authUsername, credentials, null);
    if (authentication == null) {
        return Response.serverError().status(Status.NOT_ACCEPTABLE).build();
    }
    log.audit("New authentication created for " + authUsername + " with provider " + provider);
    AuthenticationVO savedAuth = ObjectFactory.get(authentication, true);
    return Response.ok(savedAuth).build();
}
Also used : ErrorVO(org.olat.restapi.support.vo.ErrorVO) 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) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 9 with AuthenticationVO

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

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)

Example 10 with AuthenticationVO

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

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)

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