// Check if the token is not expired
if (Common.ACCESS_TOKEN_EXPIRED.equals(accessToken)) {
// Return the OAuth error message
OAuthResponse oauthResponse = OAuthRSResponse
.errorResponse(HttpServletResponse.SC_UNAUTHORIZED)
.setRealm(Common.RESOURCE_SERVER_NAME)
.setError(OAuthError.ResourceResponse.EXPIRED_TOKEN)
.buildHeaderMessage();
// Return the error message
return Response.status(Response.Status.UNAUTHORIZED)
.header(OAuth.HeaderType.WWW_AUTHENTICATE,
oauthResponse.getHeader(OAuth.HeaderType.WWW_AUTHENTICATE))
.build();
}
// Check if the token is sufficient
if (Common.ACCESS_TOKEN_INSUFFICIENT.equals(accessToken)) {
// Return the OAuth error message
OAuthResponse oauthResponse = OAuthRSResponse
.errorResponse(HttpServletResponse.SC_FORBIDDEN)
.setRealm(Common.RESOURCE_SERVER_NAME)
.setError(OAuthError.ResourceResponse.INSUFFICIENT_SCOPE)
.buildHeaderMessage();
// Return the error message
return Response.status(Response.Status.FORBIDDEN)
.header(OAuth.HeaderType.WWW_AUTHENTICATE,
oauthResponse.getHeader(OAuth.HeaderType.WWW_AUTHENTICATE))
.build();
}
// Return the OAuth error message
OAuthResponse oauthResponse = OAuthRSResponse
.errorResponse(HttpServletResponse.SC_UNAUTHORIZED)
.setRealm(Common.RESOURCE_SERVER_NAME)
.setError(OAuthError.ResourceResponse.INVALID_TOKEN)
.buildHeaderMessage();
//return Response.status(Response.Status.UNAUTHORIZED).build();
return Response.status(Response.Status.UNAUTHORIZED)
.header(OAuth.HeaderType.WWW_AUTHENTICATE,
oauthResponse.getHeader(OAuth.HeaderType.WWW_AUTHENTICATE))
.build();
} catch (OAuthProblemException e) {
// Check if the error code has been set
String errorCode = e.getError();
if (OAuthUtils.isEmpty(errorCode)) {
// Return the OAuth error message
OAuthResponse oauthResponse = OAuthRSResponse
.errorResponse(HttpServletResponse.SC_UNAUTHORIZED)
.setRealm(Common.RESOURCE_SERVER_NAME)
.buildHeaderMessage();
// If no error code then return a standard 401 Unauthorized response
return Response.status(Response.Status.UNAUTHORIZED)
.header(OAuth.HeaderType.WWW_AUTHENTICATE,
oauthResponse.getHeader(OAuth.HeaderType.WWW_AUTHENTICATE))
.build();
}
OAuthResponse oauthResponse = OAuthRSResponse
.errorResponse(HttpServletResponse.SC_BAD_REQUEST)
.setRealm(Common.RESOURCE_SERVER_NAME)
.setError(e.getError())
.setErrorDescription(e.getDescription())
.setErrorUri(e.getUri())
.buildHeaderMessage();
return Response.status(oauthResponse.getResponseStatus())
.header(OAuth.HeaderType.WWW_AUTHENTICATE,
oauthResponse.getHeader(OAuth.HeaderType.WWW_AUTHENTICATE))
.build();
}
}