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 / DownloaderKeycloakCredentials.java @ 47828
History | View | Annotate | Download (2.76 KB)
1 | 47828 | jjdelcerro | /*
|
---|---|---|---|
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; |
||
7 | |||
8 | import com.github.scribejava.core.model.OAuth2AccessToken; |
||
9 | import javax.json.JsonObject; |
||
10 | import org.gvsig.downloader.DownloaderAuthenticationConfig; |
||
11 | import org.gvsig.downloader.DownloaderCredentials; |
||
12 | import org.gvsig.downloader.spi.AbstractDownloaderCredentials; |
||
13 | import org.gvsig.json.Json; |
||
14 | |||
15 | /**
|
||
16 | *
|
||
17 | * @author jjdelcerro
|
||
18 | */
|
||
19 | public class DownloaderKeycloakCredentials |
||
20 | extends AbstractDownloaderCredentials
|
||
21 | implements DownloaderCredentials
|
||
22 | { |
||
23 | |||
24 | private final OAuth2AccessToken oauthToken; |
||
25 | private final String userid; |
||
26 | private long time; |
||
27 | private JsonObject jsonResponse;
|
||
28 | |||
29 | public DownloaderKeycloakCredentials(DownloaderAuthenticationConfig config, OAuth2AccessToken oauthToken, String userid, long time) { |
||
30 | super(userid, config.getServiceUrl());
|
||
31 | this.oauthToken = oauthToken;
|
||
32 | this.userid = userid;
|
||
33 | this.time = time;
|
||
34 | this.jsonResponse = null; |
||
35 | } |
||
36 | |||
37 | public OAuth2AccessToken getToken() {
|
||
38 | return oauthToken;
|
||
39 | } |
||
40 | |||
41 | @Override
|
||
42 | public String getAuthorizationToken() { |
||
43 | return "Bearer " + oauthToken.getAccessToken(); |
||
44 | } |
||
45 | |||
46 | @Override
|
||
47 | public String getUserid() { |
||
48 | return userid;
|
||
49 | } |
||
50 | |||
51 | public boolean isAccessTokenExpired() { |
||
52 | try {
|
||
53 | long now = System.currentTimeMillis(); |
||
54 | long expires_in = this.getResponseValueInt("expires_in", -1); // secs |
||
55 | if( expires_in<0 ) { |
||
56 | return true; |
||
57 | } |
||
58 | return time+(expires_in*1000) < now; |
||
59 | } catch(Exception ex) { |
||
60 | return true; |
||
61 | } |
||
62 | } |
||
63 | |||
64 | public boolean isRefreshTokenExpired() { |
||
65 | try {
|
||
66 | long now = System.currentTimeMillis(); |
||
67 | long refresh_expires_in = this.getResponseValueInt("refresh_expires_in", -1); // secs |
||
68 | if( refresh_expires_in<0 ) { |
||
69 | return true; |
||
70 | } |
||
71 | return time+(refresh_expires_in*1000) < now; |
||
72 | } catch(Exception ex) { |
||
73 | return false; |
||
74 | } |
||
75 | } |
||
76 | |||
77 | private String getResponseValueString(String name) { |
||
78 | if( this.jsonResponse == null ) { |
||
79 | this.jsonResponse = Json.createObject(this.oauthToken.getRawResponse()); |
||
80 | } |
||
81 | return this.jsonResponse.getString(name, null); |
||
82 | } |
||
83 | |||
84 | private int getResponseValueInt(String name, int defaultValue) { |
||
85 | if( this.jsonResponse == null ) { |
||
86 | this.jsonResponse = Json.createObject(this.oauthToken.getRawResponse()); |
||
87 | } |
||
88 | return this.jsonResponse.getInt(name, defaultValue); |
||
89 | } |
||
90 | |||
91 | |||
92 | |||
93 | } |