-
Notifications
You must be signed in to change notification settings - Fork 2
/
JwtBearerAuthenticationExtensions.cs
60 lines (54 loc) · 2.12 KB
/
JwtBearerAuthenticationExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System;
using Microsoft.Owin.Security.Jwt;
using Microsoft.Owin.Security.OAuth;
namespace Owin
{
/// <summary>
/// Extension methods provided by the JWT bearer token middleware.
/// </summary>
public static class JwtBearerAuthenticationExtensions
{
/// <summary>
/// Adds JWT bearer token middleware to your web application pipeline.
/// </summary>
/// <param name="app">The IAppBuilder passed to your configuration method.</param>
/// <param name="options">An options class that controls the middleware behavior.</param>
/// <returns>The original app parameter.</returns>
public static IAppBuilder UseJwtBearerAuthentication(this IAppBuilder app, JwtBearerAuthenticationOptions options)
{
if (app == null)
{
throw new ArgumentNullException("app");
}
if (options == null)
{
throw new ArgumentNullException("options");
}
JwtFormat jwtFormat = null;
if (options.TokenValidationParameters != null)
{
jwtFormat = new JwtFormat(options.TokenValidationParameters);
}
else
{
jwtFormat = new JwtFormat(options.AllowedAudiences, options.IssuerSecurityTokenProviders);
}
if (options.TokenHandler != null)
{
jwtFormat.TokenHandler = options.TokenHandler;
}
var bearerOptions = new OAuthBearerAuthenticationOptions
{
Realm = options.Realm,
Provider = options.Provider,
AccessTokenFormat = jwtFormat,
AuthenticationMode = options.AuthenticationMode,
AuthenticationType = options.AuthenticationType,
Description = options.Description
};
app.UseOAuthBearerAuthentication(bearerOptions);
return app;
}
}
}