Search in sources :

Example 26 with Authentication

use of org.olat.basesecurity.Authentication in project openolat by klemens.

the class PersonalRSSUtil method getPersonalRssLink.

/**
 * Calculates the absolute URL to the users personal rss feed
 * @param ureq
 * @return String
 */
public static String getPersonalRssLink(UserRequest ureq) {
    String token = null;
    Identity identity = ureq.getIdentity();
    BaseSecurity secManager = BaseSecurityManager.getInstance();
    Authentication auth = secManager.findAuthentication(identity, RSS_AUTH_PROVIDER);
    if (auth == null) {
        // no token found - create one
        token = RandomStringUtils.randomAlphanumeric(6);
        auth = secManager.createAndPersistAuthentication(identity, RSS_AUTH_PROVIDER, identity.getName(), token, null);
    } else {
        token = auth.getCredential();
    }
    StringBuilder sb = new StringBuilder();
    return sb.append(PersonalRSSUtil.URI_PERSONAL_CHANNEL).append(ureq.getIdentity().getName()).append("/").append(token).append("/olat.rss").toString();
}
Also used : Authentication(org.olat.basesecurity.Authentication) Identity(org.olat.core.id.Identity) BaseSecurity(org.olat.basesecurity.BaseSecurity)

Example 27 with Authentication

use of org.olat.basesecurity.Authentication 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 28 with Authentication

use of org.olat.basesecurity.Authentication in project openolat by klemens.

the class ViteroManager method getVmsUserId.

protected GetUserInfo getVmsUserId(Identity identity, boolean create) throws VmsNotAvailableException {
    int userId;
    boolean created = false;
    closeDBSessionSafely();
    Authentication authentication = securityManager.findAuthentication(identity, VMS_PROVIDER);
    if (authentication == null) {
        if (create) {
            created = true;
            userId = createVmsUser(identity);
            if (userId > 0) {
                securityManager.createAndPersistAuthentication(identity, VMS_PROVIDER, Integer.toString(userId), null, null);
            }
        } else {
            userId = -1;
        }
    } else {
        userId = Integer.parseInt(authentication.getAuthusername());
    }
    closeDBSessionSafely();
    return new GetUserInfo(created, userId);
}
Also used : GetUserInfo(org.olat.modules.vitero.model.GetUserInfo) Authentication(org.olat.basesecurity.Authentication)

Example 29 with Authentication

use of org.olat.basesecurity.Authentication in project openolat by klemens.

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)

Example 30 with Authentication

use of org.olat.basesecurity.Authentication in project openolat by klemens.

the class RestSecurityBeanImpl method generateToken.

@Override
public String generateToken(Identity identity, HttpSession session) {
    String token = UUID.randomUUID().toString();
    tokenToIdentity.put(token, identity.getKey());
    bindTokenToSession(token, session);
    Authentication auth = securityManager.findAuthentication(identity, REST_AUTH_PROVIDER);
    if (auth == null) {
        securityManager.createAndPersistAuthentication(identity, REST_AUTH_PROVIDER, identity.getName(), token, null);
    } else {
        authenticationDao.updateCredential(auth, token);
    }
    return token;
}
Also used : Authentication(org.olat.basesecurity.Authentication)

Aggregations

Authentication (org.olat.basesecurity.Authentication)82 Identity (org.olat.core.id.Identity)46 BaseSecurity (org.olat.basesecurity.BaseSecurity)16 Test (org.junit.Test)10 RestSecurityHelper.getIdentity (org.olat.restapi.security.RestSecurityHelper.getIdentity)8 AuthenticationVO (org.olat.restapi.support.vo.AuthenticationVO)8 URI (java.net.URI)6 ArrayList (java.util.ArrayList)6 Produces (javax.ws.rs.Produces)6 HttpResponse (org.apache.http.HttpResponse)6 SecurityGroup (org.olat.basesecurity.SecurityGroup)6 Locale (java.util.Locale)4 GET (javax.ws.rs.GET)4 HttpPut (org.apache.http.client.methods.HttpPut)4 AssertException (org.olat.core.logging.AssertException)4 DBRuntimeException (org.olat.core.logging.DBRuntimeException)4 Algorithm (org.olat.core.util.Encoder.Algorithm)4 TemporaryKey (org.olat.registration.TemporaryKey)4 ErrorVO (org.olat.restapi.support.vo.ErrorVO)4 UserPropertyHandler (org.olat.user.propertyhandlers.UserPropertyHandler)4