Skip to content

Commit

Permalink
feat: servlet classes that use the jakarta namespace (#1115)
Browse files Browse the repository at this point in the history
* feat: servlet classes that use the jakarta namespace

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* copyright

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
suztomo and gcf-owl-bot[bot] authored May 10, 2024
1 parent 6b62df1 commit 11d6a3c
Show file tree
Hide file tree
Showing 9 changed files with 620 additions and 0 deletions.
5 changes: 5 additions & 0 deletions google-oauth-client-appengine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2024 Google Inc.
*
* 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 com.google.api.client.extensions.appengine.auth.oauth2.jakarta;

import com.google.api.client.extensions.servlet.auth.oauth2.jakarta.AbstractAuthorizationCodeCallbackServlet;
import com.google.appengine.api.users.UserServiceFactory;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
* Simple extension of {@link AbstractAuthorizationCodeCallbackServlet} that uses the currently
* logged-in Google Account user, as directed in <a
* href="https://cloud.google.com/appengine/docs/standard/java/config/webxml#security-auth">Security
* and Authentication</a>. This uses the {@code jakarta.servlet} namespace.
*
* <p>Note that if there is no currently logged-in user, {@link #getUserId(HttpServletRequest)} will
* throw a {@link NullPointerException}. Example to require login for all pages:
*
* <pre>
* &lt;security-constraint&gt;
* &lt;web-resource-collection&gt;
* &lt;web-resource-name&gt;any&lt;/web-resource-name&gt;
* &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
* &lt;/web-resource-collection&gt;
* &lt;auth-constraint&gt;
* &lt;role-name&gt;*&lt;/role-name&gt;
* &lt;/auth-constraint&gt;
* &lt;/security-constraint&gt;
* </pre>
*
* <p>Sample usage:
*
* <pre>
* public class ServletCallbackSample extends AbstractAppEngineAuthorizationCodeCallbackServlet {
*
* &#64;Override
* protected void onSuccess(HttpServletRequest req, HttpServletResponse resp, Credential credential)
* throws ServletException, IOException {
* resp.sendRedirect("/");
* }
*
* &#64;Override
* protected void onError(
* HttpServletRequest req, HttpServletResponse resp, AuthorizationCodeResponseUrl errorResponse)
* throws ServletException, IOException {
* // handle error
* }
*
* &#64;Override
* protected String getRedirectUri(HttpServletRequest req) throws ServletException, IOException {
* GenericUrl url = new GenericUrl(req.getRequestURL().toString());
* url.setRawPath("/oauth2callback");
* return url.build();
* }
*
* &#64;Override
* protected AuthorizationCodeFlow initializeFlow() throws IOException {
* return new AuthorizationCodeFlow.Builder(BearerToken.authorizationHeaderAccessMethod(),
* new UrlFetchTransport(),
* new GsonFactory(),
* new GenericUrl("https://server.example.com/token"),
* new BasicAuthentication("s6BhdRkqt3", "7Fjfp0ZBr1KtDRbnfVdmIw"),
* "s6BhdRkqt3",
* "https://server.example.com/authorize").setCredentialStore(new AppEngineCredentialStore())
* .build();
* }
* </pre>
*
* @since 1.36.0
*/
public abstract class AbstractAppEngineAuthorizationCodeCallbackServlet
extends AbstractAuthorizationCodeCallbackServlet {

private static final long serialVersionUID = 1L;

@Override
protected String getUserId(HttpServletRequest req) throws ServletException, IOException {
// Use GAE Standard's users service to fetch the current user of the application.
return UserServiceFactory.getUserService().getCurrentUser().getUserId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2024 Google Inc.
*
* 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 com.google.api.client.extensions.appengine.auth.oauth2.jakarta;

import com.google.api.client.extensions.servlet.auth.oauth2.jakarta.AbstractAuthorizationCodeServlet;
import com.google.appengine.api.users.UserServiceFactory;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
* Simple extension of {@link AbstractAuthorizationCodeServlet} that uses the currently logged-in
* Google Account user, as directed in <a
* href="https://cloud.google.com/appengine/docs/standard/java/config/webxml#security-auth">Security
* and Authentication</a>. This uses the {@code jakarta.servlet} namespace.
*
* <p>Note that if there is no currently logged-in user, {@link #getUserId(HttpServletRequest)} will
* throw a {@link NullPointerException}. Example to require login for all pages:
*
* <pre>
* &lt;security-constraint&gt;
* &lt;web-resource-collection&gt;
* &lt;web-resource-name&gt;any&lt;/web-resource-name&gt;
* &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
* &lt;/web-resource-collection&gt;
* &lt;auth-constraint&gt;
* &lt;role-name&gt;*&lt;/role-name&gt;
* &lt;/auth-constraint&gt;
* &lt;/security-constraint&gt;
* </pre>
*
* <p>Sample usage:
*
* <pre>
* public class ServletSample extends AbstractAppEngineAuthorizationCodeServlet {
*
* &#64;Override
* protected void doGet(HttpServletRequest request, HttpServletResponse response)
* throws IOException {
* // do stuff
* }
*
* &#64;Override
* protected String getRedirectUri(HttpServletRequest req) throws ServletException, IOException {
* GenericUrl url = new GenericUrl(req.getRequestURL().toString());
* url.setRawPath("/oauth2callback");
* return url.build();
* }
*
* &#64;Override
* protected AuthorizationCodeFlow initializeFlow() throws IOException {
* return new AuthorizationCodeFlow.Builder(BearerToken.authorizationHeaderAccessMethod(),
* new UrlFetchTransport(),
* new GsonFactory(),
* new GenericUrl("https://server.example.com/token"),
* new BasicAuthentication("s6BhdRkqt3", "7Fjfp0ZBr1KtDRbnfVdmIw"),
* "s6BhdRkqt3",
* "https://server.example.com/authorize").setCredentialStore(new AppEngineCredentialStore())
* .build();
* }
* }
* </pre>
*
* @since 1.36.0
*/
public abstract class AbstractAppEngineAuthorizationCodeServlet
extends AbstractAuthorizationCodeServlet {

private static final long serialVersionUID = 1L;

@Override
protected String getUserId(HttpServletRequest req) throws ServletException, IOException {
// Use GAE Standard's users service to fetch the current user of the application.
return UserServiceFactory.getUserService().getCurrentUser().getUserId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2024 Google Inc.
*
* 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.
*/

/**
* OAuth 2.0 utilities that help simplify the authorization flow on Google App Engine. This package
* uses the {@code jakarta.servlet} namespace.
*
* @since 1.36.0
*/
package com.google.api.client.extensions.appengine.auth.oauth2.jakarta;
5 changes: 5 additions & 0 deletions google-oauth-client-servlet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
Expand Down
Loading

0 comments on commit 11d6a3c

Please sign in to comment.