Skip to content
This repository has been archived by the owner on Jan 27, 2019. It is now read-only.

Commit

Permalink
Add some user management: finders, token revocation
Browse files Browse the repository at this point in the history
  • Loading branch information
kog committed Apr 10, 2012
1 parent 609fdb0 commit f6bb7b1
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 1 deletion.
8 changes: 7 additions & 1 deletion SpikeLite.Core/AccessControl/IrcAuthenticationModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public void UpdateHost(KnownHost host)
/// </remarks>
public void ForgetHost(string hostmask)
{
KnownHost host = _cloaks.First(x => x.HostMask.Equals(hostmask, StringComparison.OrdinalIgnoreCase));
var host = _cloaks.First(x => x.HostMask.Equals(hostmask, StringComparison.OrdinalIgnoreCase));
_cloaks.Remove(host);
_hostDao.Delete(host);
}
Expand Down Expand Up @@ -236,6 +236,12 @@ public KnownHost FindHostByEmailAddress(string emailAddress, string accessToken,
&& longevityFilter(x.AccessTokenExpiration));
}

public KnownHost FindHostByAccessToken(string token)
{
return _cloaks.FirstOrDefault(x => !String.IsNullOrEmpty(x.AccessToken) &&
x.AccessToken.Equals(token));
}

#endregion
}
}
66 changes: 66 additions & 0 deletions SpikeLite.IPC.WebHost/Services/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
using System;
using System.Collections.Generic;
using System.ServiceModel;
using AutoMapper;
using SpikeLite.AccessControl;
using SpikeLite.Domain.Model.Authentication;
using System.Linq;
using SpikeLite.Domain.Persistence.Authentication;
using TransportKnownHost = SpikeLite.IPC.WebHost.Transport.KnownHost;
using TransportKnownHostMetaDatum = SpikeLite.IPC.WebHost.Transport.KnownHostMetaDatum;

namespace SpikeLite.IPC.WebHost.Services
{
Expand Down Expand Up @@ -49,6 +53,36 @@ public interface IAccountService : IConfigurableServiceHost
[OperationContract]
[SecuredOperation]
DateTime GetTokenExpiration();

/// <summary>
/// Gets a collection of <see cref="Transport.KnownHost"/> corresponding to all the users known to the system.
/// </summary>
///
/// <returns>A collection of all the hosts known to the system. If this is empty you might have configuration problems.</returns>
[OperationContract]
[SecuredOperation("manageUsers")]
TransportKnownHost[] GetAllUsers();

/// <summary>
/// Gets a user by id.
/// </summary>
///
/// <param name="id">The ID of the user to search for.</param>
///
/// <returns>A <see cref="Transport.KnownHost"/> that corresponds to the ID, if known.</returns>
[OperationContract]
[SecuredOperation("manageUsers")]
TransportKnownHost GetUserById(int id);

/// <summary>
/// Revokes an issued access token, using the primary identifier for a <see cref="KnownHost"/>.
/// </summary>
///
/// <param name="accessToken">The access token literal to revoke.</param>
[OperationContract]
[SecuredOperation("manageUsers")]
void RevokeAccessToken(string accessToken);

}

/// <summary>
Expand All @@ -57,6 +91,14 @@ public interface IAccountService : IConfigurableServiceHost
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class AccountService : AbstractUserContextAwareService, IAccountService
{
public override void Configure()
{
Mapper.CreateMap<KnownHost, TransportKnownHost>();
Mapper.CreateMap<KnownHostMetaDatum, TransportKnownHostMetaDatum>();

Mapper.AssertConfigurationIsValid();
}

public AccessFlag[] GetFlags()
{
var principal = GetPrincipal();
Expand Down Expand Up @@ -96,5 +138,29 @@ public DateTime GetTokenExpiration()
var principal = GetPrincipal();
return principal.AccessTokenExpiration.GetValueOrDefault();
}

public TransportKnownHost[] GetAllUsers()
{
var knownHostDao = GetBean<IKnownHostDao>("KnownHostDao");
return Mapper.Map<IList<KnownHost>, TransportKnownHost[]>(knownHostDao.FindAll());
}

public TransportKnownHost GetUserById(int id)
{
var knownHostDao = GetBean<IKnownHostDao>("KnownHostDao");
return Mapper.Map<KnownHost, TransportKnownHost>(knownHostDao.FindAll().FirstOrDefault(x => x.Id == id));
}

public void RevokeAccessToken(string accessToken)
{
var authenticationManager = GetBean<IrcAuthenticationModule>("IrcAuthenticationModule");
var host = authenticationManager.FindHostByAccessToken(accessToken);

if (null != host)
{
host.AccessTokenExpiration = DateTime.Now.ToUniversalTime();
authenticationManager.UpdateHost(host);
}
}
}
}
2 changes: 2 additions & 0 deletions SpikeLite.IPC.WebHost/SpikeLite.IPC.WebHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
<Compile Include="Services\AccountService.cs" />
<Compile Include="Services\MessagingService.cs" />
<Compile Include="Services\PingService.cs" />
<Compile Include="Transport\KnownHost.cs" />
<Compile Include="Transport\KnownHostMetaDatum.cs" />
<Compile Include="Transport\Person.cs" />
<Compile Include="Transport\PersonFactoid.cs" />
</ItemGroup>
Expand Down
39 changes: 39 additions & 0 deletions SpikeLite.IPC.WebHost/Transport/KnownHost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* SpikeLite C# IRC Bot
* Copyright (c) 2012 FreeNode ##Csharp Community
*
* This source is licensed under the terms of the MIT license. Please see the
* distributed license.txt for details.
*/

using System;
using SpikeLite.Domain.Model.Authentication;

namespace SpikeLite.IPC.WebHost.Transport
{
/// <summary>
/// Provides a transport model version of <see cref="Domain.Model.Authentication.KnownHost"/>.
/// </summary>
public class KnownHost
{
public long Id { get; set; }

public string HostMask { get; set; }

public HostMatchType HostMatchType { get; set; }

public AccessLevel AccessLevel { get; set; }

public KnownHostMetaDatum[] MetaData { get; set; }

public string AccessToken { get; set; }

public DateTime? AccessTokenIssueTime { get; set; }

public DateTime? AccessTokenExpiration { get; set; }

public String EmailAddress { get; set; }

public AccessFlag[] AccessFlags { get; set; }
}
}
28 changes: 28 additions & 0 deletions SpikeLite.IPC.WebHost/Transport/KnownHostMetaDatum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* SpikeLite C# IRC Bot
* Copyright (c) 2012 FreeNode ##Csharp Community
*
* This source is licensed under the terms of the MIT license. Please see the
* distributed license.txt for details.
*/

namespace SpikeLite.IPC.WebHost.Transport
{
/// <summary>
/// Provides a transport model version of the original domain model class, sans object cycles.
/// </summary>
public class KnownHostMetaDatum
{
public long Id { get; set; }

/// <summary>
/// Gets or sets the "tag" or description for this datum.
/// </summary>
public string Tag { get; set; }

/// <summary>
/// Gets or sets the value of this datum.
/// </summary>
public string Value { get; set; }
}
}

0 comments on commit f6bb7b1

Please sign in to comment.