Disable OWIN OAuthBearerAuthenticationMiddlewate on the fly - oauth-2.0

How can I disable or skip OAuthBearerAuthenticationMiddlewate execution?
Problem: for some requests I don't want to set Principal with claims, but requests have "Authorization" header with Bearer token value.
One of the solution is to create one more middleware and there is remove Authorization header from request.

I need to enable OAuth owin middleware with AuthenticationMode.Passive

Related

How to enable or modify security configuration for jhipster with thymeleaf?

I want to use thymeleaf instead of angular JS. I can use templates with out security and i can add httpsecurity for that URL, My question is that how to authorize the URL? currently I am using JWT token mechanism.
Thanks
The 401 error is expected, it means you did not provide a valid token.
When using Thymeleaf templates, JWT is not the best choice because it means you will have to write some javascript to handle it as a HTTP header which seems to goes against what you wanted to achieve (not using angular).
I'd recommend that you use cookies and so select session auth type.
Please note that you could also modify JHipster JWT code so that it uses a cookie rather than a HTTP header but this is more work than simply using session auth type?

Why does JWT need to be sent as a Bearer Token header?

I am adding JWT Auth for a completely new frontend to a legacy Rails backend.
Upon HTTP request, it seems most sources suggest I send the token back to the server via Bearer Header.
Why? What is the additional value of sending via header (bearer or basic). What can't I simply pass the JWT back to the server via .json and authenticate the token from there.
What benefit does an Authorization header give me, and moreso, what does a Bearer Authorization header give me?
I can of course simply follow everyone's example, but want to understand why. The bearer docs are verbose and hard to understand what I'm gaining over simple sending the JWT as part of the data in the request.
Thank you.
You can technically send a json body on each request with the JTW but that would be non standard behaviour (for instance, GET requests should not have a body via the spec).
A more standard way would be to provide an Authorization HTTP header.
The Authorization header is not specific to JWTs and its role is to specify an auth scheme between the client and the server.
Another way would be to include the JWT inside a cookie but that would make the behaviour browser specific while the HTTP header can be sent by virtually any HTTP client.
P.S
Bear in mind that contrary to Auth cookies which are sent by the browser automatically the Authorization header needs to be set by the client explicitly.

Q: Token based auth API and Javascript, can you protect other clientside files from being accessed?

Question
If you would use a similar setup as the following examples:
Simple WebAPI
Javascript OIDCClient and usermanager
Would it be possible to protect other clientside files from being accessed? Say for example i have a directory with certain files which you need a certain role to be able to access them.
Would it be possible to protect my SPA from being accessed before logging in?
Or is there a better solution which would have you end up with a protected api, folders/files on a server, SPA and a silent renew mechanism like there is in the OIDCClient?
#dmccaffery helped me out by answering my question, here is his answer for those of you who are interested.
To summarize using the OIDCClient for an SPA is certainly the way to go. Exposing stuff which needs authorization should be done by using an API. Protecting parts of your Angular App can be done using a Route guard.
The way it works is as follows:
The access token is either a JWT or a bearer token (usually) and is
added by the oidc client to every HTTP request in an authorization
header — when a web API receives a reques, the bearer token
authorization middleware will parse this HTTP header and will call the
token introspection endpoint (and potentially the user info endpoint)
to have the token validated and the users claims retrieved… if the
token was manipulated by the client, it will not be valid, and an HTTP
error will be returned (usually a 403). If the token was valid, a
claims identity is created and assigned to the http request context.
The API will now have a thread with an identity already assigned to it
that represents that user.
Also he pointed out 2 pluralsight courses which would probably be useful:
https://www.pluralsight.com/courses/building-securing-restful-api-aspdotnet
https://www.pluralsight.com/courses/oauth2-openid-connect-angular-aspdotnet

JWT token and Owin authentication

I have MVC front end application using WebApi 2 application for authentication and authorization. I am using JWT tokens for the same. So far I have been able to successfully authenticate and receive back a token… I can further access the restricted resource ([Authorize] attribute) by adding an Authorization token to the header using POSTMAN tool.
Authorization: “Bearer <jwt.token.string>”
Issue is, I am not able to intercept the call in MVC pipeline to add the token to the httpHeader. It always routes me back to the login page. Not the case when I use the POSTMAN tool. I have unsuccessfully tried injecting the token at following points:
Extending Authorize attribute with custom implementation
Adding a custom ActionFilterAttribute
Adding custom DelegatingHandler
Owin pipeline using StageMarker (PipelineStageAuthenticate) in Startup.cs
In all above cases I am hitting the event because I can debug. I have strong suspicion that I am hitting the authorization point before I set my header but I can’t figure out sequence of flow to properly intercept the HttpContext object and inject the Authorization header.
After successful authentication, add
var ctx = Request.GetOwinContext();
var authenticateResult = await ctx.Authentication.AuthenticateAsync(DefaultAuthenticationTypes.ExternalBearer);
ctx.Authentication.SignOut(DefaultAuthenticationTypes.ExternalBearer);
var applicationCookieIdentity = new ClaimsIdentity(authenticateResult.Identity.Claims, DefaultAuthenticationTypes.ApplicationCookie);
ctx.Authentication.SignIn(applicationCookieIdentity);
This will create a signed cookie and your Authorize attribute will automatically read the cookie. All your requests will become authorized subsequently.

Store JWT token in cookie

This is my setup:
1 authentication server which gives out JWT token on successfull
authentication.
Multiple API resource servers which gives information (when the user
is authenticated).
Now I want to build my ASP.NET MVC frontend. Is it ok to take the token, which I receive after authentication, and put it in a cookie so I can access it with every secured call I need to make? I use the RestSharp DLL for doing my http calls. If it has a security flaw, then where should I store my token?
I would use this code for the cookie:
System.Web.HttpContext.Current.Response.Cookies.Add(new System.Web.HttpCookie("Token")
{
Value = token.access_token,
HttpOnly = true
});
You’re on the right path! The cookie should always have the HttpOnly flag, setting this flag will prevent the JavaScript environment (in the web browser) from accessing the cookie. This is the best way to prevent XSS attacks in the browser.
You should also use the Secure flag in production, to ensure that the cookie is only sent over HTTPS.
You also need to prevent CSRF attacks. This is typically done by setting a value in another cookie, which must be supplied on every request.
I work at Stormpath and we’ve written a lot of information about front-end security. These two posts may be useful for understanding all the facets:
Token Based Authentication for Single Page Apps (SPAs)
https://stormpath.com/blog/build-secure-user-interfaces-using-jwts/
Are you generating your own JWTs?
If yes, you should consider using a signing algorithm based on asymetric encryption, like "RS256" or "RS512" -- this way you can verify the claims in your client application without sharing the private secret.
Do you really need to pass the JWT into the Cookie?
It might be safer to just put a random id in your Cookie, which references the JWT access token, and do the de-referencing magic on the server which serves your web-app.

Resources