One final change I made and so not sure if the framework can be
altered to accommodate change so I don't have to hack it...
Because I want the API user to specify the "realm" into which they
will be accessing my api (really this is a database internally), if
they were to just call one of my API methods without first calling /
auth/basic, instead of the default behavior which is to present the
login dialog (which is funny because this is what I added to the
BasicAuthProvider), I want the user to be informed to call "/auth/
basic" with the proper realm identifier. To accomplish, I changed the
code in AuthenticateAttribute.cs, Execute method like so:
public override void Execute(IHttpRequest req, IHttpResponse res,
object requestDto)
{
if (AuthService.AuthProviders == null) throw new
InvalidOperationException("The AuthService must be initialized by
calling "
+ "AuthService.Init to use an authenticate attribute");
var matchingOAuthConfigs = AuthService.AuthProviders.Where(x =>
this.Provider.IsNullOrEmpty()
|| x.Provider == this.Provider).ToList();
if (matchingOAuthConfigs.Count == 0)
{
res.WriteError(req, requestDto, "No OAuth Configs found matching
{0} provider"
.Fmt(this.Provider ?? "any"));
res.Close();
return;
}
var userPass = req.GetBasicAuthUserAndPassword();
if (userPass != null)
{
var authService = req.TryResolve<AuthService>();
authService.RequestContext = new HttpRequestContext(req, res,
requestDto);
var response = authService.Post(new Auth.Auth {
provider = BasicAuthProvider.Name,
UserName = userPass.Value.Key,
Password = userPass.Value.Value
});
}
using (var cache = req.GetCacheClient())
{
var sessionId = req.GetSessionId();
var session = sessionId != null ? cache.GetSession(sessionId) :
null;
if (session == null || !matchingOAuthConfigs.Any(x =>
session.IsAuthorized(x.Provider)))
{
// CCB HACK!
throw HttpError.Unauthorized("Not authenticated.
Please call /auth/ebsi/?realm=<database> first.");
// res.StatusCode = (int)HttpStatusCode.Unauthorized;
// res.AddHeader(HttpHeaders.WwwAuthenticate, "{0} realm=\"{1}\""
// .Fmt(matchingOAuthConfigs[0].Provider,
matchingOAuthConfigs[0].AuthRealm));
// res.Close();