Hi guys,
I’m having a strange behaviour using custom BearerToken authentication. To clarify, method “A” called using user password login execute in 65ms, while using BearerToken execute in 2 seconds. Any idea?
This is my app host
var auth = new AuthFeature(() => new CustomAuthUserSession(),
new IAuthProvider[]
{
new PinAuthProvider(),
new UserNameAuthProvider(),
new AutomaticAuthProvider(),
new CustomApiKeyAuthProvider(appSettings){
RequireSecureConnection = false
}
}
);
auth.AllowGetAuthenticateRequests = req => true;
Plugins.Add(auth);
and my CustomApiKeyAuthProvider is this
public class CustomApiKeyAuthProvider : ApiKeyAuthProvider
{
public CustomApiKeyAuthProvider(IAppSettings appSettings) : base(appSettings)
{
}
public override object Authenticate(IServiceBase authService, IAuthSession session, Authenticate request)
{
var repository = new ServiceRepository();
var user = repository.UserDecriptedVerify(request.Password);
if (user != null && user.DoExpiryDate > DateTime.Now)
{
var sessionBag = authService.GetSessionBag();
sessionBag.Set(user);
session.IsAuthenticated = true;
return new AuthenticateResponse
{
UserId = user.Id.ToString(),
UserName = user.UserCode,
SessionId = session.Id,
DisplayName = user.UserCode,
ResponseStatus = new ResponseStatus(),
BearerToken = request.Password,
Meta = new Dictionary<string, string>()
{
["CultureCode"] = user.PreferredCultureCode
}
};
}
throw new AuthenticationException(ErrorMessages.UserNotExists.Localize(authService.Request));
}
protected override ApiKey GetApiKey(IRequest req, string apiKey)
{
var key = apiKey;
if (string.IsNullOrWhiteSpace(key) || key == "undefined")
key = req.Headers.Get("ApiKey");
if (key == null || string.IsNullOrWhiteSpace(key))
return null;
return new ApiKey() { Id = key };
}
}
the method used to authenticate the token in the database is this
public User UserDecriptedVerify(string pin)
{
using (var ctx = this.OpenDbConnection())
{
var q = ctx.From<User>()
.Where(u => u.Pin == pin)
.Take(1);
return ctx.Single<User>(q);
}
}
What am I doing wrong?
Thanks
Enrico