Package org.apache.jackrabbit.api.security.authentication.token

Examples of org.apache.jackrabbit.api.security.authentication.token.TokenCredentials


            }
            return false;
        }

        public TokenCredentials getCredentials() {
            TokenCredentials tc = new TokenCredentials(token);
            for (String name : mandatoryAttributes.keySet()) {
                tc.setAttribute(name, mandatoryAttributes.get(name));
            }
            for (String name : publicAttributes.keySet()) {
                tc.setAttribute(name, publicAttributes.get(name));
            }
            return tc;
        }
View Full Code Here


     */
    public boolean authenticate(Credentials credentials) throws RepositoryException {
        if (!(credentials instanceof TokenCredentials)) {
            throw new RepositoryException("TokenCredentials expected. Cannot handle " + credentials.getClass().getName());
        }
        TokenCredentials tokenCredentials = (TokenCredentials) credentials;

        // credentials without userID -> check if attributes provide
        // sufficient information for successful authentication.
        if (token.equals(tokenCredentials.getToken())) {
            long loginTime = new Date().getTime();
            // test if the token has already expired
            if (expiry < loginTime) {
                // already expired -> login fails.
                // ... remove the expired token node before aborting the login
                removeToken();
                return false;
            }

            // test for matching key
            if (key != null && !key.equals(getDigestedKey(tokenCredentials))) {
                return false;
            }

            // check if all other required attributes match
            for (String name : attributes.keySet()) {
                if (!attributes.get(name).equals(tokenCredentials.getAttribute(name))) {
                    // no match -> login fails.
                    return false;
                }
            }

            // update set of informative attributes on the credentials
            // based on the properties present on the token node.
            Collection<String> attrNames = Arrays.asList(tokenCredentials.getAttributeNames());
            for (String key : info.keySet()) {
                if (!attrNames.contains(key)) {
                    tokenCredentials.setAttribute(key, info.get(key));
                }
            }

            // update token node if required: optionally resetting the expiration
            updateTokenNode(expiry, loginTime);
View Full Code Here

        Principal pr = user.getPrincipal();
        if (pr instanceof ItemBasedPrincipal) {
            userPath = ((ItemBasedPrincipal) pr).getPath();
        }

        TokenCredentials tokenCredentials;
        if (userPath != null && session.nodeExists(userPath)) {
            Node userNode = session.getNode(userPath);
            Node tokenParent;
            if (!userNode.hasNode(TOKENS_NODE_NAME)) {
                userNode.addNode(TOKENS_NODE_NAME, TOKENS_NT_NAME);
                try {
                    session.save();
                } catch (RepositoryException e) {
                    // may happen when .tokens node is created concurrently
                    session.refresh(false);
                }
            }
            tokenParent = userNode.getNode(TOKENS_NODE_NAME);

            long creationTime = new Date().getTime();
            long expirationTime = creationTime + tokenExpiration;

            Calendar cal = GregorianCalendar.getInstance();
            cal.setTimeInMillis(creationTime);

            // generate key part of the login token
            String key = generateKey(8);

            // create the token node
            String tokenName = Text.replace(ISO8601.format(cal), ":", ".");
            Node tokenNode;
            // avoid usage of sequential nodeIDs
            if (System.getProperty(NodeIdFactory.SEQUENTIAL_NODE_ID) == null) {
                tokenNode = tokenParent.addNode(tokenName);
            } else {
                tokenNode = ((NodeImpl) tokenParent).addNodeWithUuid(tokenName, NodeId.randomId().toString());
            }

            StringBuilder sb = new StringBuilder(tokenNode.getIdentifier());
            sb.append(DELIM).append(key);

            String token = sb.toString();
            tokenCredentials = new TokenCredentials(token);
            credentials.setAttribute(TOKEN_ATTRIBUTE, token);

            // add key property
            tokenNode.setProperty(TOKEN_ATTRIBUTE_KEY, getDigestedKey(key));

            // add expiration time property
            cal.setTimeInMillis(expirationTime);
            tokenNode.setProperty(TOKEN_ATTRIBUTE_EXPIRY, session.getValueFactory().createValue(cal));

            // add additional attributes passed in by the credentials.
            for (String name : credentials.getAttributeNames()) {
                if (!TOKEN_ATTRIBUTE.equals(name)) {
                    String value = credentials.getAttribute(name).toString();
                    tokenNode.setProperty(name, value);
                    tokenCredentials.setAttribute(name, value);
                }
            }
            session.save();
            return tokenCredentials;
        } else {
View Full Code Here

                    }
                }
            }
            Set<TokenCredentials> tokenCreds = session.getSubject().getPublicCredentials(TokenCredentials.class);
            if (!tokenCreds.isEmpty()) {
                TokenCredentials tc = tokenCreds.iterator().next();
                for (String name : tc.getAttributeNames()) {
                    if (!TokenBasedAuthentication.isMandatoryAttribute(name)) {
                        session.setAttribute(name, tc.getAttribute(name));
                    }
                }
            }

            log.debug("User {} logged in to workspace {}",
View Full Code Here

            return false;
        }

        Credentials credentials = getCredentials();
        if (credentials instanceof TokenCredentials) {
            TokenCredentials tc = (TokenCredentials) credentials;
            TokenAuthentication authentication = new TokenAuthentication(tokenProvider);
            if (authentication.authenticate(tc)) {
                tokenCredentials = tc;
                tokenInfo = authentication.getTokenInfo();
                userID = tokenInfo.getUserId();
View Full Code Here

        if (tokenProvider != null && sharedState.containsKey(SHARED_KEY_CREDENTIALS)) {
            Credentials shared = getSharedCredentials();
            if (shared != null && tokenProvider.doCreateToken(shared)) {
                TokenInfo ti = tokenProvider.createToken(shared);
                if (ti != null) {
                    TokenCredentials tc = new TokenCredentials(ti.getToken());
                    Map<String, String> attributes = ti.getPrivateAttributes();
                    for (String name : attributes.keySet()) {
                        tc.setAttribute(name, attributes.get(name));
                    }
                    attributes = ti.getPublicAttributes();
                    for (String name : attributes.keySet()) {
                        tc.setAttribute(name, attributes.get(name));
                    }
                    subject.getPublicCredentials().add(tc);
                }
            }
        }
View Full Code Here

    //-----------------------------------------------------< Authentication >---
    @Override
    public boolean authenticate(Credentials credentials) {
        boolean success = false;
        if (credentials instanceof TokenCredentials) {
            TokenCredentials tc = (TokenCredentials) credentials;
            success = validateCredentials(tc);
        }
        return success;
    }
View Full Code Here

                    }
                }
            }
            Set<TokenCredentials> tokenCreds = session.getSubject().getPublicCredentials(TokenCredentials.class);
            if (!tokenCreds.isEmpty()) {
                TokenCredentials tc = tokenCreds.iterator().next();
                for (String name : tc.getAttributeNames()) {
                    if (!TokenBasedAuthentication.isMandatoryAttribute(name)) {
                        session.setAttribute(name, tc.getAttribute(name));
                    }
                }
            }

            log.debug("User {} logged in to workspace {}",
View Full Code Here

            Configuration.setConfiguration(ConfigurationUtil.getJackrabbit2Configuration(ConfigurationParameters.EMPTY));
            if (creds instanceof SimpleCredentials) {
                SimpleCredentials sc = (SimpleCredentials) creds;
                sc.setAttribute(".token", "");
                repository.login(sc).logout();
                creds = new TokenCredentials(sc.getAttribute(".token").toString());
            } else {
                throw new UnsupportedOperationException();
            }
        }
        return creds;
View Full Code Here

    }

    @Test
    public void testAuthenticateInvalidCredentials() throws Exception {
        List<Credentials> invalid = new ArrayList<Credentials>();
        invalid.add(new TokenCredentials("token"));
        invalid.add(new Credentials() {});

        for (Credentials creds : invalid) {
            assertFalse(authentication.authenticate(creds));
        }
View Full Code Here

TOP

Related Classes of org.apache.jackrabbit.api.security.authentication.token.TokenCredentials

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.