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();
}
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();
}
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);
}
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();
}
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;
}
Aggregations