Search in sources :

Example 1 with Subject

use of org.eclipse.che.commons.subject.Subject in project che by eclipse.

the class ServerContainerInitializeListener method createSecurityContext.

protected SecurityContext createSecurityContext(final HandshakeRequest req) {
    //todo: get somehow from request
    final boolean isSecure = false;
    final String authType = "BASIC";
    final Subject subject = EnvironmentContext.getCurrent().getSubject();
    final Principal principal = new SimplePrincipal(subject.getUserName());
    return new SecurityContext() {

        @Override
        public Principal getUserPrincipal() {
            return principal;
        }

        @Override
        public boolean isUserInRole(String role) {
            return false;
        }

        @Override
        public boolean isSecure() {
            return isSecure;
        }

        @Override
        public String getAuthenticationScheme() {
            return authType;
        }
    };
}
Also used : SecurityContext(javax.ws.rs.core.SecurityContext) SimpleSecurityContext(org.everrest.core.tools.SimpleSecurityContext) Subject(org.eclipse.che.commons.subject.Subject) SimplePrincipal(org.everrest.core.tools.SimplePrincipal) Principal(java.security.Principal) SimplePrincipal(org.everrest.core.tools.SimplePrincipal)

Example 2 with Subject

use of org.eclipse.che.commons.subject.Subject in project che by eclipse.

the class OAuthAuthenticationService method invalidate.

@DELETE
@Path("token")
public void invalidate(@Required @QueryParam("oauth_provider") String oauthProvider) throws BadRequestException, NotFoundException, ServerException, ForbiddenException {
    OAuthAuthenticator oauth = getAuthenticator(oauthProvider);
    final Subject subject = EnvironmentContext.getCurrent().getSubject();
    try {
        if (!oauth.invalidateToken(subject.getUserId())) {
            throw new NotFoundException("OAuth token for user " + subject.getUserId() + " was not found");
        }
    } catch (IOException e) {
        throw new ServerException(e.getMessage());
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) NotFoundException(org.eclipse.che.api.core.NotFoundException) IOException(java.io.IOException) Subject(org.eclipse.che.commons.subject.Subject) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 3 with Subject

use of org.eclipse.che.commons.subject.Subject in project che by eclipse.

the class FactoryEditValidatorImplTest method setCurrentUser.

private void setCurrentUser(String userId) {
    Subject subject = mock(Subject.class);
    when(subject.getUserId()).thenReturn(userId);
    EnvironmentContext.getCurrent().setSubject(subject);
}
Also used : Subject(org.eclipse.che.commons.subject.Subject)

Example 4 with Subject

use of org.eclipse.che.commons.subject.Subject in project che by eclipse.

the class EnvironmentContextTest method shouldReturnAnonymousSubjectWhenThereIsNoSubject.

@Test
public void shouldReturnAnonymousSubjectWhenThereIsNoSubject() {
    //given
    EnvironmentContext expected = EnvironmentContext.getCurrent();
    expected.setSubject(null);
    //when
    Subject actualSubject = EnvironmentContext.getCurrent().getSubject();
    //then
    assertEquals(actualSubject.getUserName(), Subject.ANONYMOUS.getUserName());
    assertEquals(actualSubject.getUserId(), Subject.ANONYMOUS.getUserId());
    assertEquals(actualSubject.getToken(), Subject.ANONYMOUS.getToken());
    assertEquals(actualSubject.isTemporary(), Subject.ANONYMOUS.isTemporary());
    assertEquals(actualSubject.isAnonymous(), Subject.ANONYMOUS.isAnonymous());
}
Also used : Subject(org.eclipse.che.commons.subject.Subject) Test(org.testng.annotations.Test)

Example 5 with Subject

use of org.eclipse.che.commons.subject.Subject in project che by eclipse.

the class EnvironmentContextTest method shouldBeAbleToSetEnvContextInSameThread.

@Test
public void shouldBeAbleToSetEnvContextInSameThread() {
    //given
    EnvironmentContext expected = EnvironmentContext.getCurrent();
    expected.setSubject(new SubjectImpl("user", "id", "token", false));
    EnvironmentContext actual = EnvironmentContext.getCurrent();
    Subject actualSubject = actual.getSubject();
    assertEquals(actualSubject.getUserName(), "user");
    assertEquals(actualSubject.getUserId(), "id");
    assertEquals(actualSubject.getToken(), "token");
    assertFalse(actualSubject.isTemporary());
}
Also used : SubjectImpl(org.eclipse.che.commons.subject.SubjectImpl) Subject(org.eclipse.che.commons.subject.Subject) Test(org.testng.annotations.Test)

Aggregations

Subject (org.eclipse.che.commons.subject.Subject)8 SubjectImpl (org.eclipse.che.commons.subject.SubjectImpl)3 IOException (java.io.IOException)2 Path (javax.ws.rs.Path)2 NotFoundException (org.eclipse.che.api.core.NotFoundException)2 ServerException (org.eclipse.che.api.core.ServerException)2 EnvironmentContext (org.eclipse.che.commons.env.EnvironmentContext)2 Test (org.testng.annotations.Test)2 Principal (java.security.Principal)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpSession (javax.servlet.http.HttpSession)1 DELETE (javax.ws.rs.DELETE)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 SecurityContext (javax.ws.rs.core.SecurityContext)1 AccountImpl (org.eclipse.che.account.spi.AccountImpl)1 OAuthToken (org.eclipse.che.api.auth.shared.dto.OAuthToken)1 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)1 SimplePrincipal (org.everrest.core.tools.SimplePrincipal)1 SimpleSecurityContext (org.everrest.core.tools.SimpleSecurityContext)1