-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add user session management rest API endpoint #16
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
...ain/java/org/wso2/carbon/identity/local/auth/api/endpoint/impl/SessionApiServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
* Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. | ||
* | ||
* WSO2 Inc. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.wso2.carbon.identity.local.auth.api.endpoint.impl; | ||
|
||
import org.apache.commons.codec.digest.DigestUtils; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.apache.cxf.jaxrs.ext.MessageContext; | ||
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils; | ||
import org.wso2.carbon.identity.local.auth.api.endpoint.SessionApiService; | ||
import org.wso2.carbon.identity.local.auth.api.endpoint.dto.AllSessionsDTO; | ||
import org.wso2.carbon.identity.local.auth.api.endpoint.util.AuthAPIEndpointUtil; | ||
import org.wso2.carbon.identity.user.session.exception.SessionManagementClientException; | ||
import org.wso2.carbon.identity.user.session.exception.SessionManagementException; | ||
import org.wso2.carbon.identity.user.session.model.UserSession; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.ws.rs.core.Response; | ||
|
||
import static org.wso2.carbon.identity.local.auth.api.endpoint.util.AuthAPIEndpointUtil.getSessionManager; | ||
import static org.wso2.carbon.identity.local.auth.api.endpoint.util.AuthAPIEndpointUtil.getSessionResponse; | ||
import static org.wso2.carbon.identity.user.session.constant.SessionConstants.ErrorMessages.ERROR_CODE_NO_AUTH_USER_FOUND; | ||
import static org.wso2.carbon.identity.user.session.constant.SessionConstants.ErrorMessages.ERROR_CODE_SESSION_ID_INVALID; | ||
import static org.wso2.carbon.identity.user.session.constant.SessionConstants.ErrorMessages.ERROR_CODE_UNEXPECTED; | ||
import static org.wso2.carbon.identity.user.session.constant.SessionConstants.ErrorMessages.ERROR_CODE_USER_NOT_AUTHORIZED; | ||
|
||
public class SessionApiServiceImpl extends SessionApiService { | ||
|
||
private static final Log log = LogFactory.getLog(SessionApiServiceImpl.class); | ||
|
||
@Override | ||
public Response getUserSession(MessageContext context) { | ||
try { | ||
String sessionId = getSessionContextKey(context.getHttpServletRequest()); | ||
if (sessionId != null) { | ||
UserSession[] userSessions = getSessionManager().viewSession(sessionId); | ||
AllSessionsDTO sessionResponse = getSessionResponse(userSessions); | ||
return Response.ok().entity(sessionResponse).build(); | ||
} | ||
return Response.ok().build(); | ||
} catch (SessionManagementClientException e) { | ||
return handleBadRequestResponse(e); | ||
} catch (SessionManagementException e) { | ||
return handleServerErrorResponse(e); | ||
} catch (Throwable throwable) { | ||
return handleUnexpectedServerError(throwable); | ||
} | ||
} | ||
|
||
@Override | ||
public Response terminateASession(MessageContext context, String sessionId) { | ||
try { | ||
String currentSessionId = getSessionContextKey(context.getHttpServletRequest()); | ||
boolean terminateSession = getSessionManager().terminateASession(currentSessionId, sessionId); | ||
return Response.ok().entity(terminateSession).build(); | ||
} catch (SessionManagementClientException e) { | ||
return handleBadRequestResponse(e); | ||
} catch (SessionManagementException e) { | ||
return handleServerErrorResponse(e); | ||
} catch (Throwable throwable) { | ||
return handleUnexpectedServerError(throwable); | ||
} | ||
} | ||
|
||
@Override | ||
public Response terminateAllSessions(MessageContext context) { | ||
try { | ||
String sessionId = getSessionContextKey(context.getHttpServletRequest()); | ||
boolean terminateSession = getSessionManager().terminateAllSession(sessionId); | ||
return Response.ok().entity(terminateSession).build(); | ||
} catch (SessionManagementClientException e) { | ||
return handleBadRequestResponse(e); | ||
} catch (SessionManagementException e) { | ||
return handleServerErrorResponse(e); | ||
} catch (Throwable throwable) { | ||
return handleUnexpectedServerError(throwable); | ||
} | ||
} | ||
|
||
/** | ||
* Method to get sessionContextKey from http request. | ||
* | ||
* @param request Http request | ||
* @return SessionContextKey of particular session. | ||
*/ | ||
private String getSessionContextKey(HttpServletRequest request) { | ||
String commonAuthCookie; | ||
String sessionContextKey = null; | ||
if (FrameworkUtils.getAuthCookie(request) != null) { | ||
commonAuthCookie = FrameworkUtils.getAuthCookie(request).getValue(); | ||
if (commonAuthCookie != null) { | ||
sessionContextKey = DigestUtils.sha256Hex(commonAuthCookie); | ||
} | ||
} | ||
return sessionContextKey; | ||
} | ||
|
||
private Response handleBadRequestResponse(SessionManagementClientException e) { | ||
|
||
if (isForbiddenError(e)) { | ||
throw AuthAPIEndpointUtil.buildForbiddenException(e.getMessage(), e.getErrorCode(), log, e); | ||
} | ||
if (isNotFoundError(e)) { | ||
throw AuthAPIEndpointUtil.buildNotFoundRequestException(e.getMessage(), e.getErrorCode(), log, e); | ||
} | ||
throw AuthAPIEndpointUtil.buildBadRequestException(e.getMessage(), e.getErrorCode(), log, e); | ||
} | ||
|
||
private boolean isForbiddenError(SessionManagementClientException e) { | ||
|
||
return ERROR_CODE_NO_AUTH_USER_FOUND.getCode().equals(e.getErrorCode()) || | ||
ERROR_CODE_USER_NOT_AUTHORIZED.getCode() | ||
.equals(e.getErrorCode()); | ||
} | ||
|
||
private boolean isNotFoundError(SessionManagementClientException e) { | ||
|
||
return ERROR_CODE_SESSION_ID_INVALID.getCode().equals(e.getErrorCode()); | ||
} | ||
|
||
private Response handleServerErrorResponse(SessionManagementException e) { | ||
|
||
throw AuthAPIEndpointUtil.buildInternalServerErrorException(e.getErrorCode(), log, e); | ||
} | ||
|
||
private Response handleUnexpectedServerError(Throwable e) { | ||
|
||
throw AuthAPIEndpointUtil.buildInternalServerErrorException(ERROR_CODE_UNEXPECTED.getCode(), log, e); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move the version as a property to the parent pom
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved with 78b6e0a