Google App Engine/JavaでjunaioのChannelを作ってみる

  • このエントリーをはてなブックマークに追加
  • 0

認証Filter「JunaioAuthFilter」のソース

package org.tarotaro.java.junaio.filter;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Date;
import java.util.logging.Logger;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Hex;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

public class JunaioAuthFilter implements Filter {
private long AUTH_DATE_TOLERANCE;
private String JUNAIO_KEY = “”;

private static final long DEFAULT_AUTH_DATE_TOLERANCE = 15 * 60 * 1000;
private static final Logger log = Logger.getLogger(JunaioAuthFilter.class.getName());

@Override
public void destroy() {

}

@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)arg0;
HttpServletResponse resp = (HttpServletResponse)arg1;

AuthResult result = this.checkAuthentication(req);
if (result != AuthResult.AUTH_OK) {
log.warning(“auth check failure(” + result.toString() + “)”);
resp.sendError(HttpServletResponse.SC_UNAUTHORIZED);
resp.flushBuffer();
return;
} else {
chain.doFilter(arg0, arg1);
}
}

@Override
public void init(FilterConfig arg0) throws ServletException {
JUNAIO_KEY = arg0.getInitParameter(“JUNAIO_KEY”);
try {
AUTH_DATE_TOLERANCE = Long.parseLong(arg0.getInitParameter(“AUTH_DATE_TOLERANCE”));
}catch(Exception e) {
log.warning(e.getMessage());
AUTH_DATE_TOLERANCE = DEFAULT_AUTH_DATE_TOLERANCE;
}
}

private AuthResult checkAuthentication(HttpServletRequest req) throws ServletException {
String auth = req.getHeader(“Authorization”);
if (auth == null) {
log.warning(“auth is null”);
return AuthResult.AUTH_HEADER_NULL;
}

if (auth.indexOf(“junaio”) != 0) {
log.warning(“auth not starts with junaio error[” + auth + “]”);
return AuthResult.AUTH_HEADER_NOT_JUNAIO;
}
long dateHeader = req.getDateHeader(“Date”);
long today = new Date().getTime();
if (Math.abs(dateHeader – today) > AUTH_DATE_TOLERANCE) {
log.warning(“auth date tolerance error[” + dateHeader + “:” + today + “]”);
return AuthResult.AUTH_DATE_TOLERANCE_OVER;
}
String[] tokens = auth.split(” “);
if (tokens.length < 2 || tokens[1].length()==0) { log.warning("token length error[" + auth + "]"); return AuthResult.AUTH_DATE_TOLERANCE_OVER; } try { byte[] requestSignature = Base64.decodeBase64(tokens[1].trim()); String reqSigStr = new String(requestSignature); String queryString = req.getQueryString(); String uri = req.getRequestURI(); if (queryString != null && !queryString.equals("")) { uri += "?" + queryString; } String key1Str = JUNAIO_KEY + req.getMethod() + "\n" + uri + "\n" + "Date: " + req.getHeader("Date") + "\n"; MessageDigest md = MessageDigest.getInstance("SHA-1"); byte[] key1Digest = md.digest(key1Str.getBytes()); String key2Str = JUNAIO_KEY + Hex.encodeHexString(key1Digest); byte[] serverRequestSignature = md.digest(key2Str.getBytes()); String servReqSigStr = Hex.encodeHexString(serverRequestSignature); return reqSigStr.equals(servReqSigStr) ? AuthResult.AUTH_OK : AuthResult.AUTH_EXPIRED; } catch (NoSuchAlgorithmException e) { log.severe(e.getMessage()); throw new ServletException(e); } } private enum AuthResult { AUTH_OK(0), AUTH_HEADER_NULL(1), AUTH_HEADER_NOT_JUNAIO(2), AUTH_DATE_TOLERANCE_OVER(3), INVALID_AUTH_TOKEN(4), AUTH_EXPIRED(5); private int value; private AuthResult(int val) { this.value = val; } @Override public String toString() { return name() + "(" + this.value + ")"; } } } [/java]

web.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  7.     <filter>
  8.         <filter-name>junaio-auth</filter-name>
  9.         <filter-class>org.tarotaro.java.junaio.filter.JunaioAuthFilter</filter-class>
  10.         <init-param>
  11.             <param-name>JUNAIO_KEY</param-name>
  12.             <param-value>APIキー</param-value>
  13.         </init-param>
  14.         <init-param>
  15.             <param-name>AUTH_DATE_TOLERANCE</param-name>
  16.             <param-value>900000</param-value>
  17.         </init-param>
  18.     </filter>
  19.     <filter-mapping>
  20.         <filter-name>junaio-auth</filter-name>
  21.         <url-pattern>/jsample/*</url-pattern>
  22.     </filter-mapping>
  23.     <servlet>
  24.         <servlet-name>junaio</servlet-name>
  25.         <servlet-class>org.tarotaro.java.junaio.JunaioServlet</servlet-class>
  26.     </servlet>
  27.     <servlet-mapping>
  28.         <servlet-name>junaio</servlet-name>
  29.         <url-pattern>/jsample/*</url-pattern>
  30.     </servlet-mapping>
  31.     <welcome-file-list>
  32.         <welcome-file>index.html</welcome-file>
  33.     </welcome-file-list>
  34. </web-app>

appengine-web.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
  3.     <application>アプリの名前</application>
  4.     <version>1</version>
  5.    
  6.     <static-files>
  7.         <include path="/resources/*" />
  8.         <include path="/resources/model/*" />
  9.     </static-files>
  10.     <!-- Configure java.util.logging -->
  11.     <system-properties>
  12.         <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
  13.     </system-properties>
  14.    
  15. </appengine-web-app>
スポンサーリンク
スポンサーリンク
  • このエントリーをはてなブックマークに追加

フォローする

スポンサーリンク
スポンサーリンク