diff --git a/.github/workflows/mockatoo-oauth2-login.yml b/.github/workflows/mockatoo-oauth2-login.yml new file mode 100644 index 000000000..dd60bb522 --- /dev/null +++ b/.github/workflows/mockatoo-oauth2-login.yml @@ -0,0 +1,42 @@ +name: Mockatoo OAuth2 login + +on: + push: + paths: + - '.github/workflows/mockatoo-oauth2-login.yml' + - 'servlet/spring-boot/java/oauth2/login/**' + pull_request: + paths: + - '.github/workflows/mockatoo-oauth2-login.yml' + - 'servlet/spring-boot/java/oauth2/login/**' + +jobs: + test: + name: OAuth2 login integration test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Set up JDK + uses: actions/setup-java@de7274f081f381c8f8158605e0321c36c376e2e6 # v6.0.1 + with: + java-version: '21' + distribution: 'temurin' + cache: 'gradle' + - name: Run Mockatoo integration test + env: + MOCKATOO_TEST_EMAIL: ${{ secrets.MOCKATOO_TEST_EMAIL }} + MOCKATOO_TEST_PASSWORD: ${{ secrets.MOCKATOO_TEST_PASSWORD }} + run: | + if [ -z "$MOCKATOO_TEST_EMAIL" ] || [ -z "$MOCKATOO_TEST_PASSWORD" ]; then + echo "::notice::Mockatoo credentials are unavailable; the hosted sign-in test will be skipped." + fi + ./gradlew :servlet:spring-boot:java:oauth2:login:integrationTest --no-daemon --info + if [ -n "$MOCKATOO_TEST_EMAIL" ] && [ -n "$MOCKATOO_TEST_PASSWORD" ]; then + result_file="servlet/spring-boot/java/oauth2/login/build/integTest-results/TEST-example.MockatooOAuth2LoginApplicationTests.xml" + sign_in_result=$(grep 'name="signInWithGitHubWhenMockatooProfileActiveThenShowsAuthenticatedPage()"' "$result_file" || true) + if [ -z "$sign_in_result" ] || echo "$sign_in_result" | grep -q '> * <> * <> +* <> [[spring-login]] == Login with Spring Authorization Server @@ -408,3 +409,17 @@ Click on the Okta link, and you are then redirected to Okta for authentication. After authenticating with your Okta account credentials, the OAuth Client retrieves your email address and basic profile information from the https://openid.net/specs/openid-connect-core-1_0.html#UserInfo[UserInfo Endpoint] and establishes an authenticated session. + +[[mockatoo-login]] +== Login with Mockatoo + +For local development and integration tests, the `mockatoo` Spring profile points the Google and GitHub registrations at https://mockatoo.globalunderdog.com[Mockatoo], a free hosted mock OAuth provider. It uses the shared `mockatoo` / `mockatoo-secret` client, so no provider application registration is required. + +The default profile and the real-provider configuration remain unchanged. Start the sample with: + +[source,shell] +---- +./gradlew bootRun --args='--spring.profiles.active=mockatoo' +---- + +Open http://127.0.0.1:8080 and choose Google or GitHub. Sign in with a Mockatoo test account, then authorize the client. diff --git a/servlet/spring-boot/java/oauth2/login/src/integTest/java/example/MockatooOAuth2LoginApplicationTests.java b/servlet/spring-boot/java/oauth2/login/src/integTest/java/example/MockatooOAuth2LoginApplicationTests.java new file mode 100644 index 000000000..e729cc913 --- /dev/null +++ b/servlet/spring-boot/java/oauth2/login/src/integTest/java/example/MockatooOAuth2LoginApplicationTests.java @@ -0,0 +1,97 @@ +/* + * Copyright 2002-2026 the original author or authors. + * + * 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 + * + * https://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 example; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + +import org.htmlunit.WebClient; +import org.htmlunit.html.HtmlButton; +import org.htmlunit.html.HtmlForm; +import org.htmlunit.html.HtmlInput; +import org.htmlunit.html.HtmlPage; +import org.htmlunit.html.HtmlPasswordInput; +import org.junit.jupiter.api.Test; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.server.LocalServerPort; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT, properties = "spring.profiles.active=mockatoo") +class MockatooOAuth2LoginApplicationTests { + + private static final String MOCKATOO_ORIGIN = "https://app.mockatoo.globalunderdog.com"; + + @LocalServerPort + int port; + + @Test + void requestAuthorizeGoogleClientWhenMockatooProfileActiveThenRedirectsToMockatoo() throws Exception { + assertAuthorizationRedirect("google", "/google/o/oauth2/v2/auth"); + } + + @Test + void requestAuthorizeGitHubClientWhenMockatooProfileActiveThenRedirectsToMockatoo() throws Exception { + assertAuthorizationRedirect("github", "/github/login/oauth/authorize"); + } + + @Test + void signInWithGitHubWhenMockatooProfileActiveThenShowsAuthenticatedPage() throws Exception { + String email = System.getenv("MOCKATOO_TEST_EMAIL"); + String password = System.getenv("MOCKATOO_TEST_PASSWORD"); + assumeTrue(email != null && !email.isBlank() && password != null && !password.isBlank(), + "MOCKATOO_TEST_EMAIL and MOCKATOO_TEST_PASSWORD are required for the hosted sign-in test"); + + try (WebClient webClient = new WebClient()) { + HtmlPage login = webClient.getPage("http://127.0.0.1:" + this.port + "/oauth2/authorization/github"); + HtmlForm loginForm = login.getForms().get(0); + HtmlInput emailInput = loginForm.getInputByName("email"); + HtmlPasswordInput passwordInput = (HtmlPasswordInput) loginForm.getInputByName("password"); + emailInput.setValueAttribute(email); + passwordInput.setValueAttribute(password); + HtmlButton loginButton = (HtmlButton) loginForm.getElementsByTagName("button").get(0); + HtmlPage consent = loginButton.click(); + + HtmlButton authorizeButton = consent.getFirstByXPath("//button[normalize-space(.)='Authorize']"); + HtmlPage home = authorizeButton.click(); + + assertThat(home.asNormalizedText()).contains("You are successfully logged in", + "via the OAuth 2.0 Client GitHub"); + } + } + + private void assertAuthorizationRedirect(String registrationId, String expectedPath) throws Exception { + HttpClient httpClient = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NEVER).build(); + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create("http://127.0.0.1:" + this.port + "/oauth2/authorization/" + registrationId)) + .GET() + .build(); + HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.discarding()); + + assertThat(response.statusCode()).isEqualTo(302); + URI location = URI.create(response.headers().firstValue("Location").orElseThrow()); + assertThat(location.getScheme() + "://" + location.getAuthority() + location.getPath()) + .isEqualTo(MOCKATOO_ORIGIN + expectedPath); + assertThat(location.getQuery()).contains("client_id=mockatoo"); + } + +} diff --git a/servlet/spring-boot/java/oauth2/login/src/main/resources/application-mockatoo.yml b/servlet/spring-boot/java/oauth2/login/src/main/resources/application-mockatoo.yml new file mode 100644 index 000000000..25ed3f70c --- /dev/null +++ b/servlet/spring-boot/java/oauth2/login/src/main/resources/application-mockatoo.yml @@ -0,0 +1,18 @@ +spring: + security: + oauth2: + client: + registration: + google: + client-id: mockatoo + client-secret: mockatoo-secret + github: + client-id: mockatoo + client-secret: mockatoo-secret + provider: + google: + issuer-uri: https://app.mockatoo.globalunderdog.com/google + github: + authorization-uri: https://app.mockatoo.globalunderdog.com/github/login/oauth/authorize + token-uri: https://app.mockatoo.globalunderdog.com/github/login/oauth/access_token + user-info-uri: https://app.mockatoo.globalunderdog.com/github/api/v3/user