Search in sources :

Example 66 with BaseSecurity

use of org.olat.basesecurity.BaseSecurity in project OpenOLAT by OpenOLAT.

the class CoreSpringFactoryTest method testGetImpl.

@Test
public void testGetImpl() {
    long start = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        BaseSecurity securityManager = CoreSpringFactory.getImpl(BaseSecurity.class);
        Assert.assertNotNull(securityManager);
    }
    log.info("Get bean by impl takes (ms): " + (System.currentTimeMillis() - start));
    long start2 = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        BaseSecurity securityManager = (BaseSecurity) CoreSpringFactory.getBean("baseSecurityManager");
        Assert.assertNotNull(securityManager);
    }
    log.info("Get by by ID takes (ms): " + (System.currentTimeMillis() - start2));
}
Also used : BaseSecurity(org.olat.basesecurity.BaseSecurity) Test(org.junit.Test)

Example 67 with BaseSecurity

use of org.olat.basesecurity.BaseSecurity 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 68 with BaseSecurity

use of org.olat.basesecurity.BaseSecurity 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 69 with BaseSecurity

use of org.olat.basesecurity.BaseSecurity in project OpenOLAT by OpenOLAT.

the class UserAuthenticationWebService method changePassword.

/**
 * Change the password of a user.
 *
 * @response.representation.200.doc The password successfully changed
 * @response.representation.304.doc The password was not changed
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @response.representation.404.doc The identity or the authentication not found
 * @param username The username of the user to change the password
 * @param newPassword The new password
 * @param request The HTTP request
 * @return <code>Response</code> object. The operation status (success or fail)
 */
@POST
@Path("password")
public Response changePassword(@PathParam("username") String username, @FormParam("newPassword") String newPassword, @Context HttpServletRequest request) {
    if (!isAdmin(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    Identity doer = getIdentity(request);
    if (doer == null) {
        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();
    }
    OLATAuthManager authManager = CoreSpringFactory.getImpl(OLATAuthManager.class);
    boolean ok = authManager.changePassword(doer, identity, newPassword);
    return (ok ? Response.ok() : Response.notModified()).build();
}
Also used : OLATAuthManager(org.olat.login.auth.OLATAuthManager) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) Identity(org.olat.core.id.Identity) BaseSecurity(org.olat.basesecurity.BaseSecurity) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 70 with BaseSecurity

use of org.olat.basesecurity.BaseSecurity in project OpenOLAT by OpenOLAT.

the class Path method getFeedBaseUri.

/**
 * Returns a podcast base URI of the type<br>
 * http://myolat.org/olat/[podcast|blog]/[IDKEY/TOKEN]/ORESID
 *
 * @param feed
 * @param identityKey
 * @return The feed base uri for the given user (identity)
 */
public static String getFeedBaseUri(Feed feed, Identity identity, Long courseId, String nodeId) {
    BaseSecurity manager = BaseSecurityManager.getInstance();
    boolean isCourseNode = courseId != null && nodeId != null;
    final String slash = "/";
    StringBuffer uri = new StringBuffer();
    uri.append(Settings.getServerContextPathURI());
    uri.append(slash);
    uri.append(FeedMediaDispatcher.uriPrefixes.get(feed.getResourceableTypeName()));
    uri.append(slash);
    if (isCourseNode) {
        uri.append(COURSE_NODE_INDICATOR);
        uri.append(slash);
    }
    if (identity != null) {
        // The identity can be null for guests
        String idKey = identity.getKey().toString();
        Authentication authentication = manager.findAuthenticationByAuthusername(idKey, TOKEN_PROVIDER);
        if (authentication == null) {
            // Create an authentication
            String token = RandomStringUtils.randomAlphanumeric(6);
            authentication = manager.createAndPersistAuthentication(identity, TOKEN_PROVIDER, idKey, token, null);
        }
        // If the repository entry allows guest access it is public, thus not
        // private.
        boolean isPrivate = true;
        RepositoryEntry entry = RepositoryManager.getInstance().lookupRepositoryEntry(feed, false);
        if (entry != null && entry.getAccess() == RepositoryEntry.ACC_USERS_GUESTS) {
            isPrivate = false;
        }
        if (isPrivate) {
            // identity key
            uri.append(idKey);
            uri.append(slash);
            // token
            uri.append(authentication.getCredential());
            uri.append(slash);
        }
    }
    if (isCourseNode) {
        uri.append(courseId);
        uri.append(slash);
        uri.append(nodeId);
        uri.append(slash);
    }
    // feed id
    uri.append(feed.getResourceableId());
    // Append base uri delimiter. (Used to identify the root path for caching)
    uri.append("/_");
    return uri.toString();
}
Also used : Authentication(org.olat.basesecurity.Authentication) RepositoryEntry(org.olat.repository.RepositoryEntry) BaseSecurity(org.olat.basesecurity.BaseSecurity)

Aggregations

BaseSecurity (org.olat.basesecurity.BaseSecurity)116 Identity (org.olat.core.id.Identity)88 Path (javax.ws.rs.Path)48 RestSecurityHelper.getIdentity (org.olat.restapi.security.RestSecurityHelper.getIdentity)28 PUT (javax.ws.rs.PUT)24 Produces (javax.ws.rs.Produces)22 SecurityGroup (org.olat.basesecurity.SecurityGroup)20 RepositoryEntry (org.olat.repository.RepositoryEntry)20 DELETE (javax.ws.rs.DELETE)14 Authentication (org.olat.basesecurity.Authentication)14 MailPackage (org.olat.core.util.mail.MailPackage)14 RepositoryManager (org.olat.repository.RepositoryManager)14 Consumes (javax.ws.rs.Consumes)12 WebApplicationException (javax.ws.rs.WebApplicationException)12 CertificatesManager (org.olat.course.certificate.CertificatesManager)10 OLATResource (org.olat.resource.OLATResource)10 OLATResourceManager (org.olat.resource.OLATResourceManager)10 ArrayList (java.util.ArrayList)8 GET (javax.ws.rs.GET)8 IdentitiesAddEvent (org.olat.admin.securitygroup.gui.IdentitiesAddEvent)8