Implement authorization against the Users database and issue token based on password validation.
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using MarketData.DataAccess;
|
||||
using MarketData.Extensions;
|
||||
using MarketData.MarketDataModel.User;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace MarketDataServer.Authorization
|
||||
@@ -8,15 +10,14 @@ namespace MarketDataServer.Authorization
|
||||
public class Authorizations
|
||||
{
|
||||
private Dictionary<String, String> authorizationDictionary = null;
|
||||
private List<String> validUsers = null;
|
||||
private static Authorizations authorizations = null;
|
||||
private bool isEnabled=true;
|
||||
|
||||
private Authorizations()
|
||||
{
|
||||
validUsers = new List<String>();
|
||||
authorizationDictionary=new Dictionary<String,String>();
|
||||
validUsers.Add("sean");
|
||||
}
|
||||
|
||||
public static Authorizations GetInstance()
|
||||
{
|
||||
lock (typeof(Authorizations))
|
||||
@@ -25,11 +26,13 @@ namespace MarketDataServer.Authorization
|
||||
}
|
||||
return authorizations;
|
||||
}
|
||||
|
||||
public bool IsEnabled
|
||||
{
|
||||
get{return isEnabled;}
|
||||
set{isEnabled=value;}
|
||||
}
|
||||
|
||||
public bool IsAuthorized(String token)
|
||||
{
|
||||
lock (this)
|
||||
@@ -38,6 +41,7 @@ namespace MarketDataServer.Authorization
|
||||
return authorizationDictionary.ContainsKey(token);
|
||||
}
|
||||
}
|
||||
|
||||
public String GetAuthenticationToken()
|
||||
{
|
||||
lock (this)
|
||||
@@ -47,13 +51,24 @@ namespace MarketDataServer.Authorization
|
||||
return token;
|
||||
}
|
||||
}
|
||||
public bool IsValidUser(String user)
|
||||
|
||||
public bool IsValidUser(String username,String password)
|
||||
{
|
||||
lock (this)
|
||||
{
|
||||
return validUsers.Any(x => x.Equals(user));
|
||||
if(!UserDA.UserExists(username))return false;
|
||||
Console.WriteLine(String.Format("[{0:G}][User {1} exists.]",DateTime.Now ,username));
|
||||
User user = UserDA.GetUser(username);
|
||||
if(null == user)
|
||||
{
|
||||
Console.WriteLine(String.Format("[{0:G}][User {1} validation failed.]",DateTime.Now ,username));
|
||||
return false;
|
||||
}
|
||||
Console.WriteLine(String.Format("[{0:G}][User {1} is validated.]",DateTime.Now ,username));
|
||||
return user.Verify(password);
|
||||
}
|
||||
}
|
||||
|
||||
public static String Xor(String input, int magic)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
Reference in New Issue
Block a user