Skip to content

Commit d44ac10

Browse files
author
JureZe
committed
Keycloak api and keyclak example
1 parent 73c2ef4 commit d44ac10

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.github.scribejava.apis;
2+
3+
import com.github.scribejava.apis.openid.OpenIdJsonTokenExtractor;
4+
import com.github.scribejava.core.builder.api.DefaultApi20;
5+
import com.github.scribejava.core.builder.api.OAuth2SignatureType;
6+
import com.github.scribejava.core.extractors.TokenExtractor;
7+
import com.github.scribejava.core.model.OAuth2AccessToken;
8+
9+
public class KeyloackApi extends DefaultApi20 {
10+
11+
private String baseUrl = "http://localhost:8080";
12+
private String realm = "master";
13+
14+
public void setBaseUrl(String baseUrl) {
15+
this.baseUrl = baseUrl + (baseUrl.endsWith("/")? "" : "/");
16+
}
17+
18+
public void setRealm(String realm) {
19+
this.realm = realm;
20+
}
21+
22+
protected KeyloackApi() {
23+
}
24+
25+
private static class InstanceHolder {
26+
private static final KeyloackApi INSTANCE = new KeyloackApi();
27+
}
28+
29+
public static KeyloackApi instance() {
30+
return KeyloackApi.InstanceHolder.INSTANCE;
31+
}
32+
33+
@Override
34+
public String getAccessTokenEndpoint() {
35+
return baseUrl + "auth/realms/" + realm + "/protocol/openid-connect/token";
36+
}
37+
38+
@Override
39+
protected String getAuthorizationBaseUrl() {
40+
return baseUrl + "auth/realms/" + realm + "/protocol/openid-connect/auth";
41+
}
42+
43+
@Override
44+
public OAuth2SignatureType getSignatureType() {
45+
return OAuth2SignatureType.BEARER_AUTHORIZATION_REQUEST_HEADER_FIELD;
46+
}
47+
48+
@Override
49+
public TokenExtractor<OAuth2AccessToken> getAccessTokenExtractor() {
50+
return OpenIdJsonTokenExtractor.instance();
51+
}
52+
53+
@Override
54+
public String getRevokeTokenEndpoint() {
55+
throw new RuntimeException("Not implemented yet");
56+
}
57+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.github.scribejava.apis.examples;
2+
3+
import com.github.scribejava.apis.KeyloackApi;
4+
import com.github.scribejava.core.builder.ServiceBuilder;
5+
import com.github.scribejava.core.model.OAuth2AccessToken;
6+
import com.github.scribejava.core.model.OAuthRequest;
7+
import com.github.scribejava.core.model.Response;
8+
import com.github.scribejava.core.model.Verb;
9+
import com.github.scribejava.core.oauth.OAuth20Service;
10+
11+
import java.io.IOException;
12+
import java.util.Scanner;
13+
import java.util.concurrent.ExecutionException;
14+
15+
public final class KeyloackExample {
16+
17+
private static final String BASE_URL = "https://sicas.setcce.si";
18+
19+
private static final String REALM = "TEST_SP";
20+
21+
private static final String PROTECTED_RESOURCE_URL = BASE_URL + "/auth/realms/" + REALM + "/protocol/openid-connect/userinfo";
22+
23+
private KeyloackExample() {
24+
}
25+
26+
public static void main(String... args) throws IOException, InterruptedException, ExecutionException {
27+
// Replace these with your own api key and secret
28+
final String apiKey = "TEST_SP";
29+
final String apiSecret = "c9e04342-23e2-433a-a644-5af2a6c5d015";
30+
final KeyloackApi keyloackApi = KeyloackApi.instance();
31+
keyloackApi.setBaseUrl(BASE_URL);
32+
keyloackApi.setRealm(REALM);
33+
final OAuth20Service service = new ServiceBuilder(apiKey)
34+
.apiSecret(apiSecret)
35+
.scope("openid")
36+
.callback("http://najdi.si")
37+
.build(keyloackApi);
38+
final Scanner in = new Scanner(System.in);
39+
40+
System.out.println("=== Keyloack's OAuth Workflow ===");
41+
System.out.println();
42+
43+
// Obtain the Authorization URL
44+
System.out.println("Fetching the Authorization URL...");
45+
final String authorizationUrl = service.getAuthorizationUrl();
46+
System.out.println("Got the Authorization URL!");
47+
System.out.println("Now go and authorize ScribeJava here:");
48+
System.out.println(authorizationUrl);
49+
System.out.println("And paste the authorization code here");
50+
System.out.print(">>");
51+
final String code = in.nextLine();
52+
System.out.println();
53+
54+
// Trade the Request Token and Verfier for the Access Token
55+
System.out.println("Trading the Request Token for an Access Token...");
56+
final OAuth2AccessToken accessToken = service.getAccessToken(code);
57+
System.out.println("Got the Access Token!");
58+
System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() + "')");
59+
System.out.println();
60+
61+
// Now let's go and ask for a protected resource!
62+
System.out.println("Now we're going to access a protected resource...");
63+
final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
64+
service.signRequest(accessToken, request);
65+
final Response response = service.execute(request);
66+
System.out.println("Got it! Lets see what we found...");
67+
System.out.println();
68+
System.out.println(response.getCode());
69+
System.out.println(response.getBody());
70+
71+
System.out.println();
72+
System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
73+
74+
}
75+
}

0 commit comments

Comments
 (0)