OLD | NEW |
1 /* | 1 /* |
2 Copyright 2013 Google Inc | 2 Copyright 2013 Google Inc |
3 | 3 |
4 Licensed under the Apache License, Version 2.0(the "License"); | 4 Licensed under the Apache License, Version 2.0(the "License"); |
5 you may not use this file except in compliance with 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 | 6 You may obtain a copy of the License at |
7 | 7 |
8 http://www.apache.org/licenses/LICENSE-2.0 | 8 http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 |
10 Unless required by applicable law or agreed to in writing, software | 10 Unless required by applicable law or agreed to in writing, software |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 /// Thread-safe OAuth 2.0 method for accessing protected resources using
an <c>access_token</c> query parameter | 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. | 54 /// as specified in http://tools.ietf.org/html/rfc6750#section-2.3. |
55 /// </summary> | 55 /// </summary> |
56 public class QueryParameterAccessMethod : IAccessMethod | 56 public class QueryParameterAccessMethod : IAccessMethod |
57 { | 57 { |
58 const string AccessTokenKey = "access_token"; | 58 const string AccessTokenKey = "access_token"; |
59 | 59 |
60 public void Intercept(HttpRequestMessage request, string accessToken
) | 60 public void Intercept(HttpRequestMessage request, string accessToken
) |
61 { | 61 { |
62 var uri = request.RequestUri; | 62 var uri = request.RequestUri; |
63 request.RequestUri = new Uri(string.Format("{0}&{1}{2}{3}", | 63 request.RequestUri = new Uri(string.Format("{0}{1}{2}={3}", |
64 uri.ToString(), AccessTokenKey, string.IsNullOrEmpty(uri.Que
ry) ? "?" : "&", | 64 uri.ToString(), string.IsNullOrEmpty(uri.Query) ? "?" : "&",
AccessTokenKey, |
65 Uri.EscapeDataString(accessToken))); | 65 Uri.EscapeDataString(accessToken))); |
66 } | 66 } |
67 | 67 |
68 public string GetAccessToken(HttpRequestMessage request) | 68 public string GetAccessToken(HttpRequestMessage request) |
69 { | 69 { |
70 var query = request.RequestUri.Query; | 70 var query = request.RequestUri.Query; |
71 if (string.IsNullOrEmpty(query)) | 71 if (string.IsNullOrEmpty(query)) |
72 { | 72 { |
73 return null; | 73 return null; |
74 } | 74 } |
75 | 75 |
76 // Remove the '?'. | 76 // Remove the '?'. |
77 query = query.Substring(1); | 77 query = query.Substring(1); |
78 foreach (var parameter in query.Split('&')) | 78 foreach (var parameter in query.Split('&')) |
79 { | 79 { |
80 var keyValue = parameter.Split('='); | 80 var keyValue = parameter.Split('='); |
81 if (keyValue[0].Equals(AccessTokenKey)) | 81 if (keyValue[0].Equals(AccessTokenKey)) |
82 { | 82 { |
83 return keyValue[1]; | 83 return keyValue[1]; |
84 } | 84 } |
85 } | 85 } |
86 return null; | 86 return null; |
87 } | 87 } |
88 } | 88 } |
89 } | 89 } |
90 } | 90 } |
OLD | NEW |