summaryrefslogtreecommitdiff
path: root/src/com/p4square/grow/ccb/CCBUserVerifier.java
blob: db10b75f22af674458f382297dad6ccdc9c465e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.p4square.grow.ccb;

import com.p4square.ccbapi.CCBAPI;
import com.p4square.ccbapi.model.GetIndividualProfilesRequest;
import com.p4square.ccbapi.model.GetIndividualProfilesResponse;
import org.apache.log4j.Logger;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.security.Verifier;

/**
 * CCBUserVerifier authenticates a user through the CCB individual_profile_from_login_password API.
 */
public class CCBUserVerifier implements Verifier {
    private static final Logger LOG = Logger.getLogger(CCBUserVerifier.class);

    private final CCBAPI mAPI;

    public CCBUserVerifier(final CCBAPI api) {
        mAPI = api;
    }

    @Override
    public int verify(Request request, Response response) {
        if (request.getChallengeResponse() == null) {
            return RESULT_MISSING; // no credentials
        }

        final String username = request.getChallengeResponse().getIdentifier();
        final char[] password = request.getChallengeResponse().getSecret();

        try {
            GetIndividualProfilesResponse resp = mAPI.getIndividualProfiles(
                    new GetIndividualProfilesRequest().withLoginPassword(username, password));

            if (resp.getIndividuals().size() == 1) {
                // Wrap the IndividualProfile up in an User and update the user on the request.
                final CCBUser user = new CCBUser(resp.getIndividuals().get(0));
                LOG.info("Successfully authenticated " + user.getIdentifier());
                request.getClientInfo().setUser(user);
                return RESULT_VALID;
            }

        } catch (Exception e) {
            LOG.error("CCB API Exception: " + e, e);
        }

        return RESULT_INVALID; // Invalid credentials
    }
}