OLD | NEW |
(Empty) | |
| 1 /* |
| 2 Copyright 2013 Google Inc |
| 3 |
| 4 Licensed under the Apache License, Version 2.0(the "License"); |
| 5 you may not use this file except in compliance with the License. |
| 6 You may obtain a copy of the License at |
| 7 |
| 8 http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
| 10 Unless required by applicable law or agreed to in writing, software |
| 11 distributed under the License is distributed on an "AS IS" BASIS, |
| 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 See the License for the specific language governing permissions and |
| 14 limitations under the License. |
| 15 */ |
| 16 |
| 17 using System; |
| 18 using System.Net.Http; |
| 19 using System.Net.Http.Headers; |
| 20 |
| 21 namespace Google.Apis.Auth.OAuth2 |
| 22 { |
| 23 /// <summary> |
| 24 /// OAuth 2.0 helper for accessing protected resources using the Bearer toke
n as specified in |
| 25 /// http://tools.ietf.org/html/rfc6750. |
| 26 /// </summary> |
| 27 public class BearerToken |
| 28 { |
| 29 /// <summary> |
| 30 /// Thread-safe OAuth 2.0 method for accessing protected resources using
the Authorization header as specified· |
| 31 /// in http://tools.ietf.org/html/rfc6750#section-2.1. |
| 32 /// </summary> |
| 33 public class AuthorizationHeaderAccessMethod : IAccessMethod |
| 34 { |
| 35 const string Schema = "Bearer"; |
| 36 |
| 37 public void Intercept(HttpRequestMessage request, string accessToken
) |
| 38 { |
| 39 request.Headers.Authorization = new AuthenticationHeaderValue(Sc
hema, accessToken); |
| 40 } |
| 41 |
| 42 public string GetAccessToken(HttpRequestMessage request) |
| 43 { |
| 44 if (request.Headers.Authorization != null && request.Headers.Aut
horization.Scheme == Schema) |
| 45 { |
| 46 return request.Headers.Authorization.Parameter; |
| 47 } |
| 48 return null; |
| 49 } |
| 50 } |
| 51 |
| 52 /// <summary> |
| 53 /// Thread-safe OAuth 2.0 method for accessing protected resources using
an <c>access_token</c> query parameter |
| 54 /// as specified in http://tools.ietf.org/html/rfc6750#section-2.3. |
| 55 /// </summary> |
| 56 public class QueryParameterAccessMethod : IAccessMethod |
| 57 { |
| 58 const string AccessTokenKey = "access_token"; |
| 59 |
| 60 public void Intercept(HttpRequestMessage request, string accessToken
) |
| 61 { |
| 62 var uri = request.RequestUri; |
| 63 request.RequestUri = new Uri(string.Format("{0}&{1}{2}{3}", |
| 64 uri.ToString(), AccessTokenKey, string.IsNullOrEmpty(uri.Que
ry) ? "?" : "&", |
| 65 Uri.EscapeDataString(accessToken))); |
| 66 } |
| 67 |
| 68 public string GetAccessToken(HttpRequestMessage request) |
| 69 { |
| 70 var query = request.RequestUri.Query; |
| 71 if (string.IsNullOrEmpty(query)) |
| 72 { |
| 73 return null; |
| 74 } |
| 75 |
| 76 // Remove the '?'. |
| 77 query = query.Substring(1); |
| 78 foreach (var parameter in query.Split('&')) |
| 79 { |
| 80 var keyValue = parameter.Split('='); |
| 81 if (keyValue[0].Equals(AccessTokenKey)) |
| 82 { |
| 83 return keyValue[1]; |
| 84 } |
| 85 } |
| 86 return null; |
| 87 } |
| 88 } |
| 89 } |
| 90 } |
OLD | NEW |