View Javadoc

1   /**
2    *       Copyright 2010 Newcastle University
3    *
4    *          http://research.ncl.ac.uk/smart/
5    *
6    * Licensed to the Apache Software Foundation (ASF) under one or more
7    * contributor license agreements.  See the NOTICE file distributed with
8    * this work for additional information regarding copyright ownership.
9    * The ASF licenses this file to You under the Apache License, Version 2.0
10   * (the "License"); you may not use this file except in compliance with
11   * the License.  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   */
21  
22  package org.apache.amber.oauth2.client.demo;
23  
24  import javax.servlet.http.Cookie;
25  import javax.servlet.http.HttpServletRequest;
26  
27  import org.apache.amber.oauth2.client.demo.exception.ApplicationException;
28  import org.apache.amber.oauth2.client.demo.model.OAuthParams;
29  import org.apache.amber.oauth2.client.demo.model.OAuthRegParams;
30  
31  /**
32   *
33   *
34   *
35   */
36  public final class Utils {
37      private Utils() {
38      }
39  
40      public static final String REDIRECT_URI = "http://localhost:8080/redirect";
41      public static final String DISCOVERY_URI = "http://localhost:8080";
42  
43      public static final String REG_TYPE_PULL = "pull";
44      public static final String REG_TYPE_PUSH = "push";
45      
46      public static final String REQUEST_TYPE_QUERY= "queryParameter";
47      public static final String REQUEST_TYPE_HEADER= "headerField";
48      public static final String REQUEST_TYPE_BODY= "bodyParameter";
49  
50      public static final String GENERIC = "generic"; 
51      
52      public static final String FACEBOOK = "facebook";
53      public static final String FACEBOOK_AUTHZ = "https://graph.facebook.com/oauth/authorize";
54      public static final String FACEBOOK_TOKEN = "https://graph.facebook.com/oauth/access_token";
55      
56      public static final String GOOGLE = "google";
57      public static final String GOOGLE_AUTHZ = "https://accounts.google.com/o/oauth2/auth";
58      public static final String GOOGLE_TOKEN = "https://accounts.google.com/o/oauth2/token";
59  
60      public static final String GOWALLA = "gowalla";
61      public static final String GOWALLA_AUTHZ = "https://gowalla.com/api/oauth/authorize";
62      public static final String GOWALLA_TOKEN = "https://gowalla.com/api/oauth/access_token";
63  
64      public static final String GITHUB = "github";
65      public static final String GITHUB_AUTHZ = "https://github.com/login/oauth/authorize";
66      public static final String GITHUB_TOKEN = "https://github.com/login/oauth/access_token";
67  
68      public static final String SMART_GALLERY = "smart_gallery";
69      public static final String SMART_GALLERY_AUTHZ = "http://localhost:8090/oauth/authorize";
70      public static final String SMART_GALLERY_TOKEN = "http://localhost:8090/oauth/token";
71      public static final String SMART_GALLERY_REGISTER = "http://localhost:8090/oauthreg/register";
72  
73      public static void validateRegistrationParams(OAuthRegParams oauthParams) throws ApplicationException {
74  
75          String regType = oauthParams.getRegistrationType();
76  
77          String name = oauthParams.getName();
78          String url = oauthParams.getUrl();
79          String description = oauthParams.getDescription();
80          StringBuffer sb = new StringBuffer();
81  
82          if (isEmpty(url)) {
83              sb.append("Application URL ");
84          }
85  
86          if (REG_TYPE_PUSH.equals(regType)) {
87              if (isEmpty(name)) {
88                  sb.append("Application Name ");
89              }
90  
91              if (isEmpty(description)) {
92                  sb.append("Application URL ");
93              }
94          } else if (!REG_TYPE_PULL.equals(regType)) {
95              throw new ApplicationException("Incorrect registration type: " + regType);
96          }
97  
98          String incorrectParams = sb.toString();
99          if ("".equals(incorrectParams)) {
100             return;
101         }
102         throw new ApplicationException("Incorrect parameters: " + incorrectParams);
103 
104     }
105 
106     public static void validateAuthorizationParams(OAuthParams oauthParams) throws ApplicationException {
107 
108 
109         String authzEndpoint = oauthParams.getAuthzEndpoint();
110         String tokenEndpoint = oauthParams.getTokenEndpoint();
111         String clientId = oauthParams.getClientId();
112         String clientSecret = oauthParams.getClientSecret();
113         String redirectUri = oauthParams.getRedirectUri();
114 
115         StringBuffer sb = new StringBuffer();
116 
117         if (isEmpty(authzEndpoint)) {
118             sb.append("Authorization Endpoint ");
119         }
120 
121         if (isEmpty(tokenEndpoint)) {
122             sb.append("Token Endpoint ");
123         }
124 
125         if (isEmpty(clientId)) {
126             sb.append("Client ID ");
127         }
128 
129         if (isEmpty(clientSecret)) {
130             sb.append("Client Secret ");
131         }
132 
133         if (!REDIRECT_URI.equals(redirectUri)) {
134             sb.append("Redirect URI");
135         }
136 
137         String incorrectParams = sb.toString();
138         if ("".equals(incorrectParams)) {
139             return;
140         }
141         throw new ApplicationException("Incorrect parameters: " + incorrectParams);
142 
143     }
144 
145     public static void validateTokenParams(OAuthParams oauthParams) throws ApplicationException {
146 
147         String authzEndpoint = oauthParams.getAuthzEndpoint();
148         String tokenEndpoint = oauthParams.getTokenEndpoint();
149         String clientId = oauthParams.getClientId();
150         String clientSecret = oauthParams.getClientSecret();
151         String redirectUri = oauthParams.getRedirectUri();
152         String authzCode = oauthParams.getAuthzCode();
153 
154         StringBuffer sb = new StringBuffer();
155 
156         if (isEmpty(authzCode)) {
157             sb.append("Authorization Code ");
158         }
159 
160         if (isEmpty(authzEndpoint)) {
161             sb.append("Authorization Endpoint ");
162         }
163 
164         if (isEmpty(tokenEndpoint)) {
165             sb.append("Token Endpoint ");
166         }
167 
168         if (isEmpty(clientId)) {
169             sb.append("Client ID ");
170         }
171 
172         if (isEmpty(clientSecret)) {
173             sb.append("Client Secret ");
174         }
175 
176         if (!REDIRECT_URI.equals(redirectUri)) {
177             sb.append("Redirect URI");
178         }
179 
180         String incorrectParams = sb.toString();
181         if ("".equals(incorrectParams)) {
182             return;
183         }
184         throw new ApplicationException("Incorrect parameters: " + incorrectParams);
185 
186     }
187 
188     public static boolean isEmpty(String value) {
189         return value == null || "".equals(value);
190     }
191 
192 
193     public static String findCookieValue(HttpServletRequest request, String key) {
194         Cookie[] cookies = request.getCookies();
195 
196         for (Cookie cookie : cookies) {
197             if (cookie.getName().equals(key)) {
198                 return cookie.getValue();
199             }
200         }
201         return "";
202     }
203 
204     public static String isIssued(String value) {
205         if (isEmpty(value)) {
206             return "(Not issued)";
207         }
208         return value;
209     }
210 }