Author: rbaxter85
Date: Wed Nov 13 00:38:43 2013
New Revision: 1541339

URL: http://svn.apache.org/r1541339
Log:
Inject expiration timeout properties into OAuth2ServiceImpl
SHINDIG-1949
Comitted for Andreas Kohn

Added:
    
shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/core/oauth2/
    
shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/core/oauth2/OAuth2ServiceImplTest.java
   (with props)
Modified:
    
shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/oauth2/OAuth2ServiceImpl.java
    
shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/SocialApiTestsGuiceModule.java

Modified: 
shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/oauth2/OAuth2ServiceImpl.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/oauth2/OAuth2ServiceImpl.java?rev=1541339&r1=1541338&r2=1541339&view=diff
==============================================================================
--- 
shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/oauth2/OAuth2ServiceImpl.java
 (original)
+++ 
shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/core/oauth2/OAuth2ServiceImpl.java
 Wed Nov 13 00:38:43 2013
@@ -18,17 +18,11 @@
  */
 package org.apache.shindig.social.core.oauth2;
 
-import java.io.IOException;
-import java.io.InputStream;
 import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Properties;
 import java.util.UUID;
 
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.commons.io.IOUtils;
-import org.apache.shindig.common.util.ResourceLoader;
 import org.apache.shindig.social.core.oauth2.OAuth2Client.ClientType;
 import org.apache.shindig.social.core.oauth2.OAuth2Types.CodeType;
 import org.apache.shindig.social.core.oauth2.OAuth2Types.ErrorType;
@@ -38,10 +32,9 @@ import org.apache.shindig.social.core.oa
 import 
org.apache.shindig.social.core.oauth2.validators.OAuth2ProtectedResourceValidator;
 import org.apache.shindig.social.core.oauth2.validators.OAuth2RequestValidator;
 
-import com.google.inject.CreationException;
 import com.google.inject.Inject;
 import com.google.inject.Singleton;
-import com.google.inject.spi.Message;
+import com.google.inject.name.Named;
 
 /**
  * A simple in-memory implementation of the OAuth 2 services.
@@ -61,13 +54,13 @@ public class OAuth2ServiceImpl implement
 
 
   @Inject
-  public OAuth2ServiceImpl(OAuth2DataService store) {
+  public OAuth2ServiceImpl(OAuth2DataService store,
+      @Named("shindig.oauth2.authCodeExpiration") long authCodeExpires,
+      @Named("shindig.oauth2.accessTokenExpiration") long accessTokenExpires) {
     this.store = store;
 
-    // TODO (Eric): properties should be injected, but getting "no 
implementation bound"
-    Properties props = readPropertyFile("shindig.properties");
-    this.authCodeExpires = 
Long.valueOf(props.getProperty("shindig.oauth2.authCodeExpiration"));
-    this.accessTokenExpires = 
Long.valueOf(props.getProperty("shindig.oauth2.accessTokenExpiration"));
+    this.authCodeExpires = authCodeExpires;
+    this.accessTokenExpires = accessTokenExpires;
 
     // TODO (Matt): validators should be injected
     authCodeValidator = new AuthorizationCodeRequestValidator(store);
@@ -79,6 +72,14 @@ public class OAuth2ServiceImpl implement
     return store;
   }
 
+  protected long getAuthCodeExpires() {
+    return authCodeExpires;
+  }
+
+  protected long getAccessTokenExpires() {
+    return accessTokenExpires;
+  }
+
   public void authenticateClient(OAuth2NormalizedRequest req)
       throws OAuth2Exception {
     OAuth2Client client = store.getClient(req.getClientId());
@@ -187,19 +188,4 @@ public class OAuth2ServiceImpl implement
   public OAuth2Code generateRefreshToken(OAuth2NormalizedRequest req) {
     throw new RuntimeException("not yet implemented");
   }
-
-  private Properties readPropertyFile(String propertyFile) {
-    Properties properties = new Properties();
-    InputStream is = null;
-    try {
-      is = ResourceLoader.openResource(propertyFile);
-      properties.load(is);
-    } catch (IOException e) {
-      throw new CreationException(Arrays.asList(
-          new Message("Unable to load properties: " + propertyFile)));
-    } finally {
-      IOUtils.closeQuietly( is );
-    }
-    return properties;
-  }
 }

Modified: 
shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/SocialApiTestsGuiceModule.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/SocialApiTestsGuiceModule.java?rev=1541339&r1=1541338&r2=1541339&view=diff
==============================================================================
--- 
shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/SocialApiTestsGuiceModule.java
 (original)
+++ 
shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/SocialApiTestsGuiceModule.java
 Wed Nov 13 00:38:43 2013
@@ -92,5 +92,9 @@ public class SocialApiTestsGuiceModule e
         .toInstance(10);
     bind(OAuth2Service.class).to(OAuth2ServiceImpl.class);
     bind(OAuth2DataService.class).to(OAuth2DataServiceImpl.class);
+
+    
bindConstant().annotatedWith(Names.named("shindig.oauth2.authCodeExpiration")).to("300000");
+    
bindConstant().annotatedWith(Names.named("shindig.oauth2.accessTokenExpiration")).to("18000000");
+    
bindConstant().annotatedWith(Names.named("shindig.oauth2.refreshTokenExpiration")).to("432000000");
   }
 }

Added: 
shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/core/oauth2/OAuth2ServiceImplTest.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/core/oauth2/OAuth2ServiceImplTest.java?rev=1541339&view=auto
==============================================================================
--- 
shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/core/oauth2/OAuth2ServiceImplTest.java
 (added)
+++ 
shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/core/oauth2/OAuth2ServiceImplTest.java
 Wed Nov 13 00:38:43 2013
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.shindig.social.core.oauth2;
+
+import static org.easymock.EasyMock.createMock;
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.inject.AbstractModule;
+import com.google.inject.Guice;
+import com.google.inject.name.Names;
+
+public class OAuth2ServiceImplTest {
+  private static final long EXPECTED_AUTHCODE_EXPIRATION = 300000;
+  private static final long EXPECTED_ACCESSTOKEN_EXPIRATION = 18000000;
+
+  private OAuth2ServiceImpl serviceImpl;
+
+  private static class OAuth2ServiceImplTestModule extends AbstractModule {
+    @Override
+    protected void configure() {
+      // Bind string values, to match what happens when reading the 
shindig.properties file.
+      
bindConstant().annotatedWith(Names.named("shindig.oauth2.authCodeExpiration"))
+          .to(Long.toString(EXPECTED_AUTHCODE_EXPIRATION));
+      
bindConstant().annotatedWith(Names.named("shindig.oauth2.accessTokenExpiration"))
+          .to(Long.toString(EXPECTED_ACCESSTOKEN_EXPIRATION));
+
+      OAuth2DataService dataService = createMock(OAuth2DataService.class);
+      bind(OAuth2DataService.class).toInstance(dataService);
+    }
+  }
+
+  @Before
+  public void setUp() {
+    serviceImpl = Guice.createInjector(new OAuth2ServiceImplTestModule())
+        .getInstance(OAuth2ServiceImpl.class);
+  }
+
+  @Test
+  public void testAccessTokenExpirationIsConfigured() {
+    assertEquals(EXPECTED_ACCESSTOKEN_EXPIRATION, 
serviceImpl.getAccessTokenExpires());
+  }
+
+  @Test
+  public void testAuthCodeExpirationIsConfigured() {
+    assertEquals(EXPECTED_AUTHCODE_EXPIRATION, 
serviceImpl.getAuthCodeExpires());
+  }
+}
\ No newline at end of file

Propchange: 
shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/core/oauth2/OAuth2ServiceImplTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to