<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jason Robert</title>
    <description>The latest articles on DEV Community by Jason Robert (@jasontrobert).</description>
    <link>https://dev.to/jasontrobert</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F132318%2Fee287c00-147c-4cac-8634-32ff4a98e1a3.jpg</url>
      <title>DEV Community: Jason Robert</title>
      <link>https://dev.to/jasontrobert</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jasontrobert"/>
    <language>en</language>
    <item>
      <title>Secure Your SPA with Authorization Code Flow with PKCE</title>
      <dc:creator>Jason Robert</dc:creator>
      <pubDate>Tue, 29 Oct 2019 21:25:44 +0000</pubDate>
      <link>https://dev.to/jasontrobert/secure-your-spa-with-authorization-code-flow-with-pkce-58cb</link>
      <guid>https://dev.to/jasontrobert/secure-your-spa-with-authorization-code-flow-with-pkce-58cb</guid>
      <description>&lt;p&gt;Single page applications (SPAs) offer many benefits over classic web applications. Among these benefits, SPAs have the ability to provide users with a rich and responsive user interface. This is largely due to the fact that much of a SPA’s application logic resides in the browser. If you’re wondering how to secure an application that primarily runs in the browser then you’ve come to the right place! In this article, we will discuss how you can leverage &lt;a href="https://openid.net/connect/"&gt;OpenID Connect&lt;/a&gt; with Angular to secure an ASP.NET Core 3.0 web application.&lt;/p&gt;

&lt;h1&gt;
  
  
  Implicit Flow
&lt;/h1&gt;

&lt;p&gt;In the past, the &lt;a href="https://tools.ietf.org/wg/oauth/"&gt;OAuth working group’s&lt;/a&gt; recommendation for securing a SPA was Implicit Flow. With &lt;a href="https://openid.net/specs/openid-connect-core-1_0.html#ImplicitFlowAuth"&gt;Implicit Flow&lt;/a&gt;, unauthenticated users are sent to an identity provider’s &lt;a href="https://tools.ietf.org/html/rfc6749#section-3.1"&gt;authorization endpoint&lt;/a&gt;. Following successful authentication, the end-user is redirected back to the client application with a token included in the url.&lt;/p&gt;

&lt;p&gt;While returning the token in the redirect url might make you feel uneasy, there isn’t much risk when only requesting an &lt;a href="https://openid.net/specs/openid-connect-core-1_0.html#ImplicitIDToken"&gt;id token&lt;/a&gt; (as shown below). Id tokens typically only contain identity data such as the user’s name, email, etc. Unlike an &lt;a href="https://tools.ietf.org/html/rfc6749#section-1.4"&gt;access token&lt;/a&gt;, id tokens are not used for authentication.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET https://some-authorization-server.com/authorize
    &amp;amp;client_id=…
    &amp;amp;response_type=id_token
    &amp;amp;redirect_uri=https://my-web-app.com/callback
    &amp;amp;scope=openid profile
    &amp;amp;state=…
    &amp;amp;nonce=…
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That said, it is very common for a SPA to call an API to interact with persistent data. In order to authenticate, an &lt;a href="https://tools.ietf.org/html/rfc6749#section-1.4"&gt;access token&lt;/a&gt; is required. Access tokens are credentials used to “access” to a secure resource. In this case, the secure resource is the API we are calling from our SPA.&lt;/p&gt;

&lt;p&gt;This is precisely where the problem lies. Returning an access token to the client in the &lt;a href="https://tools.ietf.org/html/rfc6749#section-3.1.2"&gt;redirection url&lt;/a&gt; could result in the token ending up in your browser’s history or in a referrer header.&lt;/p&gt;

&lt;p&gt;As you probably guessed due to their sensitivity, we do not want to transmit access tokens in the url if we can avoid it. This is why the &lt;a href="https://tools.ietf.org/html/draft-parecki-oauth-browser-based-apps-02#section-9.8"&gt;OAuth2 IETF&lt;/a&gt; working group now recommends using Authorization Code Flow with PKCE to secure your Single Page Applications.&lt;/p&gt;

&lt;h1&gt;
  
  
  Authorization Code Flow with PKCE
&lt;/h1&gt;

&lt;p&gt;People always ask me why Implicit Flow was recommended in the first place if Authorization Code Flow is inherently more secure. This is largely due to the fact that for many years browsers prevented JavaScript from making an HTTP request to a server that was hosted in a different domain. Implicit Flow was a way to work around this restriction by leveraging the redirection url. Tokens could be passed in the redirect URL instead of making a direct HTTP request. Fortunately, this is no longer required as the majority of modern browsers support &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS"&gt;Cross-Origin Resource Sharing (CORS)&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This new capability, opens the door for the Authorization Code Flow. Unlike Implicit Flow, Authorization Code Flow happens in two steps. First, an Authorization Code is requested.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GET https://some-authorization-server.com/authorize
    &amp;amp;client_id=…
    &amp;amp;response_type=code
    &amp;amp;redirect_uri=https://my-web-app.com/callback
    &amp;amp;scope=openid profile
    &amp;amp;code_challenge=xxxxxxxxxxxxx
    &amp;amp;code_challenge_method=S256
    &amp;amp;state=…
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Second, the code is exchanged for an access token. This is done in a separate HTTP request allowing the access token to be returned in the response body.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST https://some-authorization-server.com/token
    &amp;amp;client_id=…
    &amp;amp;code=xxxxxxxxxxxxx
    &amp;amp;grant_type=authorization_code
    &amp;amp;code_verifier=xxxxxxxxxxxxx
    &amp;amp;redirect_uri=https://my-web-app.com/callback
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Where does PCKE (Proof of Code Key Exchange) come into play? Take another look at the initial request and note the code_challenge. The code challenge is a random value that gets cryptographically generated by a code verifier. This is the same code verifier that gets sent in the secondary request to the &lt;a href="https://tools.ietf.org/html/rfc6749#section-3.2"&gt;token endpoint&lt;/a&gt;. In order for an access token to be granted, the code_verifier must match the code_challenge. This is an additional security measure. If the authorization code gets intercepted, it’s is useless without the correct code verifier.&lt;/p&gt;

&lt;h1&gt;
  
  
  Angular and Auth0
&lt;/h1&gt;

&lt;p&gt;So how do we get started? The first step is to find a suitable client library. Since OpenID Connect is merely a specification, a number of JavaScript SDKs are available. In this article, we will be using &lt;a href="https://auth0.com/"&gt;Auth0&lt;/a&gt; as our identity provider so, it makes sense to use their &lt;a href="https://github.com/auth0/auth0-spa-js"&gt;auth0-spa-js&lt;/a&gt; library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @auth0/auth0-spa-js --save
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once installed, we can get started by following the step-by-step instructions in the &lt;a href="https://auth0.com/docs/quickstart/spa/angular2"&gt;SPA quickstart guide&lt;/a&gt;. I won’t rehash these steps here however, I will point out that when using the Auth0 client library, there is no need to specify detailed OpenID Connect configuration. The library abstracts most of those details away, as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;auth0Client$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;createAuth0Client&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;domain&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;YOUR_DOMAIN&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;client_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;YOUR_CLIENT_ID&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;redirect_uri&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/callback`&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After completing the quickstart guide, you should notice a Log In button in your web application. Clicking the button redirects you to an Auth0 login screen. Using your browser’s built-in developer tools, you can see the Authorization Code Flow with PCKE in action! The initial redirect url is shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://espressocoder.auth0.com/authorize
    ?client_id=...
    &amp;amp;redirect_uri=http%3A%2F%2Flocalhost%3A4200%2Fcallback
    &amp;amp;scope=openid%20profile%20email
    &amp;amp;response_type=code
    &amp;amp;response_mode=query
    &amp;amp;state=UXVlV2FGbkpJZGRRUlQyRGJ1aHdVN1BZOWRLYWVxWS5tbHQ4NjVOQn42QQ%3D%3D
    &amp;amp;nonce=D5K7B-ZjdPiahF6wL1lfj9T-YXvxjWab12wi-Da1F6F
    &amp;amp;code_challenge=jtxUMOTQ-6Kfrxvxp-EGv20t9YmUzG2ZZIy0O7aRt1w
    &amp;amp;code_challenge_method=S256
    &amp;amp;auth0Client=eyJuYW1lIjoiYXV0aDAtc3BhLWpzIiwidmVyc2lvbiI6IjEuMy4xIn0%3D
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As we’ve seen before, the URL contains familiar query string parameters such as response_type=code as well as a code challenge. This establishes with Auth0 how we plan on retrieving our token. After we are successfully authenticated, we are returned an authorization code as a URL fragment and as we’d expect, this is followed up by a subsequent request to retrieve our access token.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://espressocoder.auth0.com/oauth/token
{
  "grant_type":"authorization_code",
  "redirect_uri":"http://localhost:4200",
  "client_id":"...",
  "code_verifier":"kzdJ7.XT4QQf.2oOfvfoXLIdTapXm2PBANx8G078PpS",
  "code":"nsg2f5jYYAMP6lnC"
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once the code and code_verifier are validated, an access_token is returned. As we’ve mentioned, this is great as we avoid returning this information in the URL. What do we do with this amazing access_token? Let’s take a look next at how we can use an access_token for authentication in ASP.NET Core 3.0.&lt;/p&gt;

&lt;h1&gt;
  
  
  Validated the Token in ASP.NET Core
&lt;/h1&gt;

&lt;p&gt;Being the defacto standard for authentication in the web, OpenID Connect is supported by most web frameworks. Of course, ASP.NET Core is no different. To start requiring an access token, only a few changes are required. First, we need to add the following lines to the ConfigureServices function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthentication(o =&amp;gt;
    {
        o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    }).AddJwtBearer(o =&amp;gt;
    {
        o.Authority = "https://espressocoder.auth0.com";
        o.Audience = "https://jrtech.oauth.samples";
    });
    services.AddControllers();
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next, we need to add the Authentication middleware to the Configure function. As you may already know, the sequence is important here so, we want to be sure to add the Authentication middleware just before the UseEndpoints middleware is added to the request pipeline.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    app.UseHttpsRedirection();
    app.UseRouting();
    app.UseAuthentication();

    app.UseAuthorization();
    app.UseEndpoints(endpoints =&amp;gt;
    {
        endpoints.MapControllers();
    });
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The last step is to identify the controllers and actions that need to be secured and decorate them with the &lt;em&gt;Authorize&lt;/em&gt; attribute. Once this is complete, making requests without a valid access token results in an 401 Unauthorized status code!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;IF YOU'VE ENJOYED THIS ARTICLE&lt;/em&gt; and want to see more content like this, please give me a follow and checkout my blog @ &lt;a href="https://espressocoder.com"&gt;https://espressocoder.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>aspnetcore</category>
      <category>angular</category>
      <category>oauth</category>
      <category>security</category>
    </item>
  </channel>
</rss>
