This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
KeycloakHelper.java
105 lines (88 loc) · 4.32 KB
/
KeycloakHelper.java
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* JBoss, Home of Professional Open Source
* Copyright Red Hat, Inc., and individual contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.aerogear.android.cookbook.shootandshare.util;
import android.app.Activity;
import org.jboss.aerogear.android.authorization.AuthorizationManager;
import org.jboss.aerogear.android.authorization.AuthzModule;
import org.jboss.aerogear.android.authorization.oauth2.OAuth2AuthorizationConfiguration;
import org.jboss.aerogear.android.authorization.oauth2.OAuthWebViewDialog;
import org.jboss.aerogear.android.cookbook.shootandshare.model.PhotoHolder;
import org.jboss.aerogear.android.core.Callback;
import org.jboss.aerogear.android.pipe.PipeManager;
import org.jboss.aerogear.android.pipe.rest.RestfulPipeConfiguration;
import org.jboss.aerogear.android.pipe.rest.multipart.MultipartRequestBuilder;
import java.io.File;
import java.net.URL;
public class KeycloakHelper {
private static final String SHOOT_SERVER_URL = "http://10.0.2.2:8080";
private static final String AUTHZ_URL = SHOOT_SERVER_URL +"/auth";
private static final String AUTHZ_ENDPOINT = "/realms/shoot-realm/protocol/openid-connect/auth";
private static final String ACCESS_TOKEN_ENDPOINT = "/realms/shoot-realm/protocol/openid-connect/token";
private static final String REFRESH_TOKEN_ENDPOINT = "/realms/shoot-realm/protocol/openid-connect/token";
private static final String AUTHZ_ACCOOUNT_ID = "keycloak-token";
private static final String AUTHZ_CLIENT_ID = "shoot-third-party";
private static final String AUTHZ_REDIRECT_URL = "http://oauth2callback";
private static final String MODULE_NAME = "KeyCloakAuthz";
static {
try {
AuthorizationManager.config(MODULE_NAME, OAuth2AuthorizationConfiguration.class)
.setBaseURL(new URL(AUTHZ_URL))
.setAuthzEndpoint(AUTHZ_ENDPOINT)
.setAccessTokenEndpoint(ACCESS_TOKEN_ENDPOINT)
.setRefreshEndpoint(REFRESH_TOKEN_ENDPOINT)
.setAccountId(AUTHZ_ACCOOUNT_ID)
.setClientId(AUTHZ_CLIENT_ID)
.setRedirectURL(AUTHZ_REDIRECT_URL)
.asModule();
PipeManager.config("kc-upload", RestfulPipeConfiguration.class).module(AuthorizationManager.getModule(MODULE_NAME))
.withUrl(new URL(SHOOT_SERVER_URL + "/shoot/rest/photos"))
.requestBuilder(new MultipartRequestBuilder())
.forClass(PhotoHolder.class);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void connect(final Activity activity, final Callback callback) {
try {
final AuthzModule authzModule = AuthorizationManager.getModule(MODULE_NAME);
authzModule.requestAccess(activity, new Callback<String>() {
@SuppressWarnings("unchecked")
@Override
public void onSuccess(String s) {
callback.onSuccess(s);
}
@Override
public void onFailure(Exception e) {
if (!e.getMessage().matches(OAuthWebViewDialog.OAuthReceiver.DISMISS_ERROR)) {
authzModule.deleteAccount();
}
callback.onFailure(e);
}
});
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
public static void upload(final File file, final Callback callback, Activity activity) {
PipeManager.getPipe("kc-upload", activity).save(new PhotoHolder(file), callback);
}
public static boolean isConnected() {
return AuthorizationManager.getModule(MODULE_NAME).isAuthorized();
}
}