This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-samples.git


The following commit(s) were added to refs/heads/master by this push:
     new ebbcfa6  chore(oauth): update to the latest OAuth client bundle changes
ebbcfa6 is described below

commit ebbcfa6beae381378f5a6232a10c97b95f3c2821
Author: Robert Munteanu <[email protected]>
AuthorDate: Thu Dec 12 17:20:56 2024 +0100

    chore(oauth): update to the latest OAuth client bundle changes
---
 .../oauth_demo/GithubRepositoriesModel.java        | 31 +++++++++++++---------
 .../oauth_demo/impl/YoutubeSearchServlet.java      | 12 ++++-----
 2 files changed, 23 insertions(+), 20 deletions(-)

diff --git 
a/oauth/core/src/main/java/org/apache/sling/samples/oauth_demo/GithubRepositoriesModel.java
 
b/oauth/core/src/main/java/org/apache/sling/samples/oauth_demo/GithubRepositoriesModel.java
index ab0e1a0..9cf9880 100644
--- 
a/oauth/core/src/main/java/org/apache/sling/samples/oauth_demo/GithubRepositoriesModel.java
+++ 
b/oauth/core/src/main/java/org/apache/sling/samples/oauth_demo/GithubRepositoriesModel.java
@@ -19,12 +19,12 @@ package org.apache.sling.samples.oauth_demo;
 import java.io.IOException;
 import java.net.URI;
 
+import javax.annotation.PostConstruct;
+
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.auth.oauth_client.ClientConnection;
-import org.apache.sling.auth.oauth_client.OAuthToken;
-import org.apache.sling.auth.oauth_client.OAuthTokenStore;
-import org.apache.sling.auth.oauth_client.OAuthUris;
-import org.apache.sling.auth.oauth_client.TokenState;
+import org.apache.sling.auth.oauth_client.OAuthTokenAccess;
+import org.apache.sling.auth.oauth_client.OAuthTokenResponse;
 import org.apache.sling.models.annotations.Model;
 import org.apache.sling.models.annotations.injectorspecific.OSGiService;
 import org.apache.sling.models.annotations.injectorspecific.SlingObject;
@@ -41,10 +41,17 @@ public class GithubRepositoriesModel {
     private ClientConnection connection;
     
     @OSGiService
-    private OAuthTokenStore tokenStore;
+    private OAuthTokenAccess tokenAccess;
+
+    private OAuthTokenResponse tokenResponse;
+    
+    @PostConstruct
+    public void initToken() {
+        tokenResponse = tokenAccess.getAccessToken(connection, request, 
request.getRequestURI());
+    }
     
     public boolean needsLogin() {
-        return getAccessToken().getState() != TokenState.VALID;
+        return !tokenResponse.hasValidToken();
     }
     
     public String getSearch() {
@@ -57,20 +64,18 @@ public class GithubRepositoriesModel {
         return searchParam != null && searchParam.trim().length() > 0;
     }
     
-    private OAuthToken getAccessToken() {
-        return tokenStore.getAccessToken(connection, 
request.getResourceResolver());
-    }
-    
     public GithubRepositoriesSearchResponse getSearchResponse() throws 
IOException, InterruptedException {
-        if ( hasSearch() ) {
-            return new 
GithubHttpClient().searchRepositories(request.getParameter("search"), 
getAccessToken().getValue());
+        if ( hasSearch() && tokenResponse.hasValidToken() ) {
+            return new 
GithubHttpClient().searchRepositories(request.getParameter("search"), 
tokenResponse.getTokenValue());
         }
         
         return null;
     }
     
     public URI getGithubLoginUrl() {
-        return OAuthUris.getOidcEntryPointUri(connection, request, 
request.getRequestURI());
+        if (needsLogin())
+            return tokenResponse.getRedirectUri();
+        return null;
     }
 
 }
diff --git 
a/oauth/core/src/main/java/org/apache/sling/samples/oauth_demo/impl/YoutubeSearchServlet.java
 
b/oauth/core/src/main/java/org/apache/sling/samples/oauth_demo/impl/YoutubeSearchServlet.java
index 0e2cd54..26c2fb9 100644
--- 
a/oauth/core/src/main/java/org/apache/sling/samples/oauth_demo/impl/YoutubeSearchServlet.java
+++ 
b/oauth/core/src/main/java/org/apache/sling/samples/oauth_demo/impl/YoutubeSearchServlet.java
@@ -26,9 +26,7 @@ import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
 import org.apache.sling.api.request.RequestDispatcherOptions;
 import org.apache.sling.auth.oauth_client.ClientConnection;
-import org.apache.sling.auth.oauth_client.OAuthToken;
-import org.apache.sling.auth.oauth_client.OAuthTokenRefresher;
-import org.apache.sling.auth.oauth_client.OAuthTokenStore;
+import org.apache.sling.auth.oauth_client.OAuthTokenAccess;
 import org.apache.sling.auth.oauth_client.support.OAuthEnabledSlingServlet;
 import org.apache.sling.samples.oauth_demo.YoutubeSearchResponse;
 import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
@@ -44,8 +42,8 @@ public class YoutubeSearchServlet extends 
OAuthEnabledSlingServlet {
     private static final long serialVersionUID = 1L;
     
     @Activate
-    public YoutubeSearchServlet(@Reference(target = "(name=google)") 
ClientConnection connection, @Reference OAuthTokenStore tokenStore, @Reference 
OAuthTokenRefresher oidcClient) {
-        super(connection, tokenStore, oidcClient);
+    public YoutubeSearchServlet(@Reference(target = "(name=google)") 
ClientConnection connection, @Reference OAuthTokenAccess tokenAccess) {
+        super(connection, tokenAccess);
     }
 
     @Override
@@ -55,13 +53,13 @@ public class YoutubeSearchServlet extends 
OAuthEnabledSlingServlet {
 
     @Override
     protected void doGetWithToken(@NotNull SlingHttpServletRequest request, 
@NotNull SlingHttpServletResponse response,
-            OAuthToken token) throws IOException, ServletException {
+            String accessToken) throws IOException, ServletException {
 
         try {
             String search = request.getParameter("search");
             if (search != null && !search.isBlank()) {
                 YoutubeHttpClient client = new YoutubeHttpClient();
-                YoutubeSearchResponse youtubeResponse = 
client.searchVideos(search, token.getValue());
+                YoutubeSearchResponse youtubeResponse = 
client.searchVideos(search, accessToken);
                 request.setAttribute("ytResponse", youtubeResponse);
             }
             

Reply via email to