forked from NJepop/Piranha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPIKeyAuthorization.cs
64 lines (57 loc) · 1.72 KB
/
APIKeyAuthorization.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* Copyright (c) 2011-2015 Håkan Edling
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*
* http://github.com/piranhacms/piranha
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Web;
using Piranha.Web;
namespace Piranha.Legacy.Services
{
/// <summary>
/// Simple authorization manager that handles API-keys.
/// </summary>
public class APIKeyAuthorization : ServiceAuthorizationManager
{
#region Members
private const string APIKEY = "apikey";
#endregion
/// <summary>
/// Checks that the given API-key is valid.
/// </summary>
/// <param name="context">The current service context</param>
/// <returns>Whether the api key is valid or not.</returns>
protected override bool CheckAccessCore(OperationContext context) {
return APIKeys.IsValidKey(GetAPIKey(context));
}
#region Private members
/// <summary>
/// Gets the API-key from the current request.
/// </summary>
/// <param name="context">The current context</param>
/// <returns>The API-key</returns>
private string GetAPIKey(OperationContext context) {
// Get the request message
var request = context.RequestContext.RequestMessage;
// Get the HTTP Request
var props = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
// Get the api key
return HttpUtility.ParseQueryString(props.QueryString)[APIKEY];
// Return as Guid
/*if (!String.IsNullOrEmpty(key))
try {
return new Guid(key) ;
} catch {}
return Guid.Empty ;*/
}
#endregion
}
}