|
| 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