Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.downloader / org.gvsig.downloader.swing / org.gvsig.downloader.swing.scribejava / src / main / java / org / gvsig / downloader / swing / scribejava / keycloak / callbacks / CallbackAuthorizationHandler.java @ 47828

History | View | Annotate | Download (3.71 KB)

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.downloader.swing.scribejava.keycloak.callbacks;
7

    
8
import com.github.scribejava.core.model.OAuth2AccessToken;
9
import com.github.scribejava.core.model.OAuth2Authorization;
10
import com.github.scribejava.core.oauth.OAuth20Service;
11
import com.sun.net.httpserver.HttpExchange;
12
import com.sun.net.httpserver.HttpHandler;
13
import java.io.IOException;
14
import javax.json.JsonObject;
15
import org.gvsig.downloader.swing.scribejava.keycloak.DownloaderAuthenticationKeycloakRequester;
16
import org.gvsig.downloader.swing.scribejava.keycloak.DownloaderKeycloakCredentials;
17

    
18
/**
19
 *
20
 * @author jjdelcerro
21
 */
22
@SuppressWarnings("UseSpecificCatch")
23
public class CallbackAuthorizationHandler extends AbstractCallback implements HttpHandler {
24

    
25

    
26
    public CallbackAuthorizationHandler(DownloaderAuthenticationKeycloakRequester identificationRequester, OAuth20Service service, String contextPath) {
27
        super(identificationRequester, service, contextPath);
28
    }
29

    
30
    @Override
31
    public void handle(HttpExchange t) throws IOException {
32
        String requestURL = "Unknown";
33
        try {
34
            long now = System.currentTimeMillis();
35
            requestURL = t.getRequestURI().toString();
36
            OAuth2Authorization auth = service.extractAuthorization(t.getRequestURI().toString());
37
            if (auth.getCode() == null) {
38
                this.identificationRequester.setCredentials(null);
39
                response(t, 200, message("Can't autthenticate user\n"));
40
                return;
41
            }
42
            final OAuth2AccessToken accessToken = service.getAccessToken(auth.getCode());
43
            this.identificationRequester.setCredentials(new DownloaderKeycloakCredentials(this.getConfig(), accessToken, null, now));
44

    
45
            String userId = null;
46
            JsonObject userinfo = this.identificationRequester.userInfo(service);
47
            if (userinfo!=null) {
48
                userId = userinfo.getString("preferred_username", null);
49
//                    System.out.println("userid: " + userId);
50
//                    System.out.println("name: " + userinfo.getString("name", null));
51
//                    System.out.println("email: " + userinfo.getString("email", null));
52
//                    System.out.println("roles: " + userinfo.get("gvsigol_roles").toString());
53
//                    System.out.println("grupos: " + userinfo.get("groups").toString());
54
            } else {
55
                userId = "unkown"+accessToken.getAccessToken().hashCode();
56
            }
57
            this.identificationRequester.setCredentials(new DownloaderKeycloakCredentials(this.getConfig(),accessToken, userId, now));
58
            response(t, 200, 
59
                    message_and_close(
60
                            (userId == null)?
61
                                "\n\n\n\n\n\n\n\n\n\n<p align=\"center\">User authenticated</p>\n\n<p align=\"center\"><button type=\"button\" onclick=\"self.close()\">Close this window</button></p>\n\n":
62
                                "\n\n\n\n\n\n\n\n\n\n<p align=\"center\">User " + userId + " authenticated</p>\n\n<p align=\"center\"><button type=\"button\" onclick=\"self.close();\">Close this window</button></p>\n\n"
63
                    )
64
            );
65
        } catch (Exception ex) {
66
            this.identificationRequester.setCredentials(null);
67
            LOGGER.warn("Can't process callback authorization (contextPath '" + contextPath + "', request URL '" + requestURL + "')", ex);
68
            responseQuietly(t, 500, message("Can't authenticate user\n" + ex.getMessage()));
69
        } finally {
70
            this.stopWaitingForResponse();
71
        }
72
    }
73
    
74
}