-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b59d001
Showing
7 changed files
with
318 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
OAuth2v1 | ||
======== | ||
|
||
A demo and helper class for providing Google OAuth2 v1 authentication in java. | ||
|
||
Assumptions | ||
|
||
- familiarity with OOP, java, maven, and jee | ||
- java application server listening on localhost:8080 | ||
|
||
Prerequisites | ||
|
||
- Google API access credentials (Client ID, Client Secret). Set it up here https://code.google.com/apis/console/ | ||
- Set up allowed Redirect URIs at Google API -> API Access. Input: http://localhost:8080/OAuth2v1/index.jsp | ||
- a positive outlook on life | ||
|
||
Usage | ||
|
||
1. Add Client ID, and Client Secret parameters to GoogleAuthHelper.java | ||
2. Compile the project ($ mvn clean install) | ||
3. Deploy war to application server | ||
4. Browse to: http://localhost:8080/OAuth2v1/ | ||
5. Click "log in with google" on top of this page |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.danter.auth.google</groupId> | ||
<artifactId>OAuth2v1</artifactId> | ||
<packaging>war</packaging> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>Demo Webapp for Google OAuth2</name> | ||
<url>http://maven.apache.org</url> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.http-client</groupId> | ||
<artifactId>google-http-client</artifactId> | ||
<version>1.13.1-beta</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.http-client</groupId> | ||
<artifactId>google-http-client-jackson</artifactId> | ||
<version>1.13.1-beta</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax</groupId> | ||
<artifactId>javaee-web-api</artifactId> | ||
<version>6.0</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.apis</groupId> | ||
<artifactId>google-api-services-oauth2</artifactId> | ||
<version>v2-rev29-1.13.2-beta</version> | ||
</dependency> | ||
</dependencies> | ||
<repositories> | ||
<repository> | ||
<id>google-api-services</id> | ||
<url>http://google-api-client-libraries.appspot.com/mavenrepo</url> | ||
</repository> | ||
</repositories> | ||
<build> | ||
<finalName>OAuth2v1</finalName> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>2.3.2</version> | ||
<configuration> | ||
<source>1.6</source> | ||
<target>1.6</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-war-plugin</artifactId> | ||
<version>2.1.1</version> | ||
<configuration> | ||
<failOnMissingWebXml>false</failOnMissingWebXml> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
|
||
</build> | ||
</project> |
88 changes: 88 additions & 0 deletions
88
src/main/java/com/danter/google/auth/GoogleAuthHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.danter.google.auth; | ||
|
||
import com.google.api.client.auth.oauth2.Credential; | ||
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; | ||
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeRequestUrl; | ||
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse; | ||
import com.google.api.client.http.GenericUrl; | ||
import com.google.api.client.http.HttpRequest; | ||
import com.google.api.client.http.HttpRequestFactory; | ||
import com.google.api.client.http.HttpTransport; | ||
import com.google.api.client.http.javanet.NetHttpTransport; | ||
import com.google.api.client.json.JsonFactory; | ||
import com.google.api.client.json.jackson.JacksonFactory; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* A helper class for Google's OAuth2 authentication API. | ||
* @version 20130224 | ||
* @author Matyas Danter | ||
*/ | ||
public final class GoogleAuthHelper { | ||
|
||
/** | ||
* Please provide a value for the CLIENT_ID constant before proceeding, set this up at https://code.google.com/apis/console/ | ||
*/ | ||
private static final String CLIENT_ID = "YOUR ID HERE"; | ||
/** | ||
* Please provide a value for the CLIENT_SECRET constant before proceeding, set this up at https://code.google.com/apis/console/ | ||
*/ | ||
private static final String CLIENT_SECRET = "SUPER SECRET SAUCE"; | ||
|
||
/** | ||
* Callback URI that google will redirect to after successful authentication | ||
*/ | ||
private static final String CALLBACK_URI = "http://localhost:8080/OAuth2v1/index.jsp"; | ||
|
||
// start google authentication constants | ||
private static final Iterable<String> SCOPE = Arrays.asList("https://www.googleapis.com/auth/userinfo.profile;https://www.googleapis.com/auth/userinfo.email".split(";")); | ||
private static final String USER_INFO_URL = "https://www.googleapis.com/oauth2/v1/userinfo"; | ||
private static final JsonFactory JSON_FACTORY = new JacksonFactory(); | ||
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); | ||
// end google authentication constants | ||
|
||
|
||
private final GoogleAuthorizationCodeFlow flow; | ||
|
||
/** | ||
* Constructor initializes the Google Authorization Code Flow with CLIENT ID, SECRET, and SCOPE | ||
*/ | ||
public GoogleAuthHelper() { | ||
flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, | ||
JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, SCOPE).build(); | ||
} | ||
|
||
/** | ||
* Builds a login URL based on client ID, secret, callback URI, and scope | ||
*/ | ||
public String buildLoginUrl() { | ||
|
||
final GoogleAuthorizationCodeRequestUrl url = flow.newAuthorizationUrl(); | ||
|
||
return url.setRedirectUri(CALLBACK_URI).setState("google").build(); | ||
} | ||
|
||
/** | ||
* Expects an Authentication Code, and makes an authenticated request for the user's profile information | ||
* @return JSON formatted user profile information | ||
* @param authCode authentication code provided by google | ||
*/ | ||
public String getUserInfoJson(final String authCode) throws IOException { | ||
|
||
final GoogleTokenResponse response = flow.newTokenRequest(authCode).setRedirectUri(CALLBACK_URI).execute(); | ||
final Credential credential = flow.createAndStoreCredential(response, null); | ||
final HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(credential); | ||
// Make an authenticated request | ||
final GenericUrl url = new GenericUrl(USER_INFO_URL); | ||
final HttpRequest request = requestFactory.buildGetRequest(url); | ||
request.getHeaders().setContentType("application/json"); | ||
final String jsonIdentity = request.execute().parseAsString(); | ||
|
||
return jsonIdentity; | ||
|
||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<!DOCTYPE web-app PUBLIC | ||
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" | ||
"http://java.sun.com/dtd/web-app_2_3.dtd" > | ||
|
||
<web-app> | ||
<display-name>Demo Webapp for Google OAuth2</display-name> | ||
</web-app> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<%@page import="com.danter.google.auth.GoogleAuthHelper"%> | ||
<%@page contentType="text/html" pageEncoding="UTF-8"%> | ||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" | ||
"http://www.w3.org/TR/html4/loose.dtd"> | ||
|
||
<html> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
<title>Google OAuth 2.0 v1 Demo</title> | ||
<style> | ||
body { | ||
font-family: Sans-Serif; | ||
margin: 1em; | ||
} | ||
.oauthDemo a { | ||
display: block; | ||
border-style: solid; | ||
border-color: #bbb #888 #666 #aaa; | ||
border-width: 1px 2px 2px 1px; | ||
background: #ccc; | ||
color: #333; | ||
line-height: 2; | ||
text-align: center; | ||
text-decoration: none; | ||
font-weight: 900; | ||
width: 13em; | ||
} | ||
.oauthDemo pre { | ||
background: #ccc; | ||
} | ||
.oauthDemo a:active { | ||
border-color: #666 #aaa #bbb #888; | ||
border-width: 2px 1px 1px 2px; | ||
color: #000; | ||
} | ||
.readme { | ||
padding: .5em; | ||
background-color: #F9AD81; | ||
color: #333; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="oauthDemo"> | ||
<% | ||
/* | ||
* The GoogleAuthHelper handles all the heavy lifting, and contains all "secrets" | ||
* required for constructing a google login url. | ||
*/ | ||
final GoogleAuthHelper helper = new GoogleAuthHelper(); | ||
if (request.getParameter("code") == null | ||
|| request.getParameter("state") == null) { | ||
/* | ||
* initial visit to the page | ||
*/ | ||
out.println("<a href='" + helper.buildLoginUrl() | ||
+ "'>log in with google</a>"); | ||
} else if (request.getParameter("code") != null | ||
&& request.getParameter("state").equals("google")) { | ||
out.println("<pre>"); | ||
/* | ||
* Executes after google redirects to the callback url. | ||
* Please note that the state request parameter is for convenience to differentiate | ||
* between authentication methods (ex. facebook oauth, google oauth, twitter, in-house). | ||
* | ||
* GoogleAuthHelper()#getUserInfoJson(String) method returns a String containing | ||
* the json representation of the authenticated user's information. | ||
* At this point you should parse and persist the info. | ||
*/ | ||
out.println(helper.getUserInfoJson(request.getParameter("code"))); | ||
out.println("</pre>"); | ||
} | ||
%> | ||
</div> | ||
<br /> | ||
<div class="readme"> | ||
<h1>Read Me First</h1> | ||
|
||
<h3>Assumptions</h3> | ||
|
||
<ul> | ||
<li>familiarity with OOP, java, maven, and jee</li> | ||
<li>java application server listening on localhost:8080</li> | ||
</ul> | ||
|
||
<h3>Prerequisites</h3> | ||
|
||
<ul> | ||
<li>Google API access credentials (Client ID, Client Secret). | ||
Set it up here <a href='https://code.google.com/apis/console/'>https://code.google.com/apis/console/</a> | ||
</li> | ||
<li>Set up allowed Redirect URIs at Google API → API | ||
Access. Input: http://localhost:8080/OAuth2v1/index.jsp</li> | ||
<li>a positive outlook on life</li> | ||
</ul> | ||
|
||
<h3>Usage</h3> | ||
|
||
<ol> | ||
<li>Add Client ID, and Client Secret parameters to <b>GoogleAuthHelper.java</b></li> | ||
<li>Compile the project (<b>$ mvn clean install</b>)</li> | ||
<li>Deploy war to application server</li> | ||
<li>Browse to: <a href="http://localhost:8080/OAuth2v1/">http://localhost:8080/OAuth2v1/</a></li> | ||
<li>Click <b>"log in with google"</b> on top of this page</li> | ||
</ol> | ||
|
||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Assumptions | ||
|
||
- familiarity with OOP, java, maven, and jee | ||
- java application server listening on localhost:8080 | ||
|
||
Prerequisites | ||
|
||
- Google API access credentials (Client ID, Client Secret). Set it up here https://code.google.com/apis/console/ | ||
- Set up allowed Redirect URIs at Google API -> API Access. Input: http://localhost:8080/OAuth2v1/index.jsp | ||
- a positive outlook on life | ||
|
||
Usage | ||
|
||
1. Add Client ID, and Client Secret parameters to GoogleAuthHelper.java | ||
2. Compile the project ($ mvn clean install) | ||
3. Deploy war to application server | ||
4. Browse to: http://localhost:8080/OAuth2v1/ | ||
5. Click "log in with google" on top of this page |