Search in sources :

Example 1 with CoreTokenException

use of com.sun.identity.coretoken.CoreTokenException in project OpenAM by OpenRock.

the class CoreTokenResource method createToken.

/**
     * Creates a token.
     *
     * @param headers HTTPHeaders object of the request.
     * @param request HTTPServletRequest object of the request.
     * @param msgBody Message body containing the JSON-encoded token attributes.
     * @return JSON-encoded token.id attribute of the new token.
     */
@POST
@Consumes("application/json")
@Produces("application/json")
public Response createToken(@Context HttpHeaders headers, @Context HttpServletRequest request, String msgBody) {
    String newTokenId = null;
    JSONObject json = null;
    try {
        json = new JSONObject(msgBody);
        String tokenVal = CoreTokenStoreFactory.getInstance().createToken(CoreTokenUtils.getAdminSubject(), json);
        // retrieve token.id attribute and set as part of Location header
        JSONObject jObj = new JSONObject(tokenVal);
        newTokenId = jObj.getJSONArray(CoreTokenConstants.TOKEN_ID).getString(0);
        Response.ResponseBuilder builder = Response.status(201);
        builder.entity(tokenVal);
        builder.type("application/json");
        builder.header("Location", request.getRequestURL() + "/" + newTokenId);
        Response retResponse = builder.build();
        // logging
        // TODO : get the request session and used in login field
        String[] data = new String[] { json.getJSONArray(CoreTokenConstants.TOKEN_TYPE).toString(), json.getJSONArray(CoreTokenConstants.TOKEN_SUBJECT).toString(), json.names().toString() };
        TokenLogUtils.access(Level.INFO, TokenLogUtils.TOKEN_CREATE_SUCCESS, data, null, newTokenId);
        return retResponse;
    } catch (JSONException ex) {
        CoreTokenUtils.debug.error("CoreTokenResource.createToken", ex);
        String[] data = null;
        if (json != null) {
            try {
                data = new String[] { ex.getLocalizedMessage(), json.getJSONArray(CoreTokenConstants.TOKEN_TYPE).toString(), json.getJSONArray(CoreTokenConstants.TOKEN_SUBJECT).toString(), json.names().toString() };
            } catch (JSONException ex1) {
            }
        } else {
            data = new String[] { ex.getLocalizedMessage(), "", "", "" };
        }
        TokenLogUtils.error(Level.INFO, TokenLogUtils.UNABLE_TO_CREATE_TOKEN, data, null, newTokenId);
        throw getWebApplicationException(ex, MimeType.PLAIN);
    } catch (CoreTokenException ce) {
        CoreTokenUtils.debug.error("CoreTokenResource.createToken", ce);
        String[] data = null;
        if (json != null) {
            try {
                data = new String[] { ce.getLocalizedMessage(), json.getJSONArray(CoreTokenConstants.TOKEN_TYPE).toString(), json.getJSONArray(CoreTokenConstants.TOKEN_SUBJECT).toString(), json.names().toString() };
            } catch (JSONException ex1) {
            }
        } else {
            data = new String[] { ce.getLocalizedMessage(), "", "", "" };
        }
        TokenLogUtils.error(Level.INFO, TokenLogUtils.UNABLE_TO_CREATE_TOKEN, data, null, newTokenId);
        throw getWebApplicationException(headers, ce);
    }
}
Also used : Response(javax.ws.rs.core.Response) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) CoreTokenException(com.sun.identity.coretoken.CoreTokenException) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 2 with CoreTokenException

use of com.sun.identity.coretoken.CoreTokenException in project OpenAM by OpenRock.

the class CoreTokenResource method updateToken.

/**
     * Updates a token.
     *
     * @param headers HTTPHeaders object of the request.
     * @param request HTTPServletRequest object of the request.
     * @param tokenId value of token.id in the request path parameter.
     * @param eTag value of the If-Match header in the request.
     * @param msgBody Message body containing the JSON-encoded token attributes.
     */
@PUT
@Consumes("application/json")
@Path("{token.id}")
public void updateToken(@Context HttpHeaders headers, @Context HttpServletRequest request, @PathParam("token.id") String tokenId, @HeaderParam("If-Match") String eTag, String msgBody) {
    try {
        JSONObject jObj = new JSONObject(msgBody);
        CoreTokenStoreFactory.getInstance().updateToken(CoreTokenUtils.getAdminSubject(), tokenId, eTag, jObj);
        // logging
        String[] data = new String[] { jObj.names().toString() };
        TokenLogUtils.access(Level.INFO, TokenLogUtils.TOKEN_UPDATE_SUCCESS, data, null, tokenId);
    } catch (CoreTokenException ce) {
        CoreTokenUtils.debug.error("CoreTokenResource.updateToken", ce);
        String[] data = new String[] { ce.getLocalizedMessage() };
        TokenLogUtils.error(Level.INFO, TokenLogUtils.UNABLE_TO_UPDATE_TOKEN, data, null, tokenId);
        throw getWebApplicationException(headers, ce);
    } catch (JSONException je) {
        CoreTokenUtils.debug.error("CoreTokenResource.updateToken", je);
        String[] data = new String[] { je.getLocalizedMessage() };
        TokenLogUtils.error(Level.INFO, TokenLogUtils.UNABLE_TO_UPDATE_TOKEN, data, null, tokenId);
        throw getWebApplicationException(je, MimeType.PLAIN);
    }
}
Also used : JSONObject(org.json.JSONObject) CoreTokenException(com.sun.identity.coretoken.CoreTokenException) JSONException(org.json.JSONException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 3 with CoreTokenException

use of com.sun.identity.coretoken.CoreTokenException in project OpenAM by OpenRock.

the class TokenCleanupRunnable method run.

public void run() {
    if (!runCleanup()) {
        // no need to run cleanup on this instance
        return;
    }
    CoreTokenUtils.debug.message("TokenCleanupRunnable.run : START");
    Set<String> tokenSet = getAllTokens();
    Iterator<String> tokens = tokenSet.iterator();
    if (CoreTokenUtils.debug.messageEnabled()) {
        CoreTokenUtils.debug.message("TokenCleanupRunnable.run : found " + tokenSet.size() + " tokens");
    }
    while (tokens.hasNext()) {
        String token = tokens.next();
        String dn = OpenSSOCoreTokenStore.getCoreTokenDN(token);
        SSOToken adminToken = (SSOToken) AccessController.doPrivileged(AdminTokenAction.getInstance());
        if (SMSEntry.checkIfEntryExists(dn, adminToken)) {
            try {
                SMSEntry s = new SMSEntry(adminToken, dn);
                String tokenExpiry = getTokenExpiry(s);
                if (CoreTokenUtils.isTokenExpired(tokenExpiry)) {
                    s.delete();
                    // add logging
                    TokenLogUtils.access(Level.INFO, TokenLogUtils.EXPIRED_TOKEN_DELETE_SUCCESS, null, null, token);
                    if (CoreTokenUtils.debug.messageEnabled()) {
                        CoreTokenUtils.debug.message("TokenCleanupRunnable" + ".run: removed expired token " + token);
                    }
                }
            } catch (SMSException ex) {
                CoreTokenUtils.debug.error("TokenCleanupRunnable.run", ex);
            } catch (SSOException ex) {
                CoreTokenUtils.debug.error("TokenCleanupRunnable.run", ex);
            } catch (CoreTokenException ce) {
                CoreTokenUtils.debug.error("TokenCleanupRunnable.run", ce);
            }
        }
    }
    CoreTokenUtils.debug.message("TokenCleanupRunnable.run : END");
}
Also used : SSOToken(com.iplanet.sso.SSOToken) SMSException(com.sun.identity.sm.SMSException) CoreTokenException(com.sun.identity.coretoken.CoreTokenException) SMSEntry(com.sun.identity.sm.SMSEntry) SSOException(com.iplanet.sso.SSOException)

Example 4 with CoreTokenException

use of com.sun.identity.coretoken.CoreTokenException in project OpenAM by OpenRock.

the class OpenSSOCoreTokenStore method updateToken.

/**
     * Updates a token.
     * @param subject caller subject.
     * @param tokenId token.id of the token to be updated.
     * @param eTag
     * @param newVals
     * @throws CoreTokenException
     * @throws JSONException
     */
public void updateToken(Subject subject, String tokenId, String eTag, JSONObject newVals) throws CoreTokenException, JSONException {
    SSOToken token = SubjectUtils.getSSOToken(subject);
    if (token == null) {
        throw new CoreTokenException(210, null, 401);
    }
    String dn = null;
    try {
        dn = getCoreTokenDN(tokenId);
        if (SMSEntry.checkIfEntryExists(dn, token)) {
            SMSEntry s = new SMSEntry(token, dn);
            String tokenAttrs = getTokenAttributeValueFromSM(s, JSON_ATTR);
            JSONObject json = new JSONObject(tokenAttrs);
            checkETag(eTag, json, tokenId);
            // validate attribute names and convert to lower case
            newVals = validateAndToLowerCase(newVals);
            // token.id attribute can't be modified
            if (newVals.has(CoreTokenConstants.TOKEN_ID)) {
                throw new CoreTokenException(221, null, 409);
            }
            // token.type attribute can't be modified
            if (newVals.has(CoreTokenConstants.TOKEN_TYPE)) {
                throw new CoreTokenException(224, null, 409);
            }
            json = updateAttributeValues(json, newVals);
            Map<String, Set<String>> map = validateAndCreateMap(tokenId, json);
            s.setAttributes(map);
            s.save();
        } else {
            throw new CoreTokenException(203, null, 404);
        }
    } catch (SMSException e) {
        CoreTokenUtils.debug.error("OpenSSOCoreTokenStore.updateToken", e);
        throw new CoreTokenException(206, null, e);
    } catch (SSOException e) {
        CoreTokenUtils.debug.error("OpenSSOCoreTokenStore.updateToken", e);
        throw new CoreTokenException(301, null, e);
    }
}
Also used : SSOToken(com.iplanet.sso.SSOToken) HashSet(java.util.HashSet) Set(java.util.Set) JSONObject(org.json.JSONObject) SMSException(com.sun.identity.sm.SMSException) CoreTokenException(com.sun.identity.coretoken.CoreTokenException) SMSEntry(com.sun.identity.sm.SMSEntry) SSOException(com.iplanet.sso.SSOException)

Example 5 with CoreTokenException

use of com.sun.identity.coretoken.CoreTokenException in project OpenAM by OpenRock.

the class OpenSSOCoreTokenStore method searchTokens.

/**
     * 
     * @param subject
     * @param queryString
     * @return JSON array of tokens matching the queryString
     * @throws CoreTokenException
     */
public JSONArray searchTokens(Subject subject, String queryString) throws CoreTokenException {
    try {
        SSOToken token = SubjectUtils.getSSOToken(subject);
        if (token == null) {
            throw new CoreTokenException(216, null, 401);
        }
        JSONArray results = new JSONArray();
        if (SMSEntry.checkIfEntryExists(SERVICE_DN, token)) {
            String filter = createSearchFilter(queryString);
            Set<String> dns = SMSEntry.search(token, SERVICE_DN, filter, 0, 0, false, false);
            for (String dn : dns) {
                if (!LDAPUtils.dnEquals(SERVICE_DN, dn)) {
                    results.put(LDAPUtils.rdnValueFromDn(dn));
                }
            }
        }
        return results;
    } catch (SMSException ex) {
        CoreTokenUtils.debug.error("OpenSSOCoreTokenStore.searchToken", ex);
        throw new CoreTokenException(215, ex);
    }
}
Also used : SSOToken(com.iplanet.sso.SSOToken) SMSException(com.sun.identity.sm.SMSException) JSONArray(org.json.JSONArray) CoreTokenException(com.sun.identity.coretoken.CoreTokenException)

Aggregations

CoreTokenException (com.sun.identity.coretoken.CoreTokenException)13 SSOToken (com.iplanet.sso.SSOToken)6 SMSException (com.sun.identity.sm.SMSException)6 JSONObject (org.json.JSONObject)6 SSOException (com.iplanet.sso.SSOException)5 SMSEntry (com.sun.identity.sm.SMSEntry)5 JSONArray (org.json.JSONArray)4 Produces (javax.ws.rs.Produces)3 JSONException (org.json.JSONException)3 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Consumes (javax.ws.rs.Consumes)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Response (javax.ws.rs.core.Response)2 StringTokenizer (java.util.StringTokenizer)1 POST (javax.ws.rs.POST)1 PUT (javax.ws.rs.PUT)1