-
Notifications
You must be signed in to change notification settings - Fork 644
/
Copy pathAzureActiveDirectoryV2AuthenticatorConfiguration.cs
57 lines (49 loc) · 2.37 KB
/
AzureActiveDirectoryV2AuthenticatorConfiguration.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Configuration;
using System.Globalization;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.OpenIdConnect;
namespace NuGetGallery.Authentication.Providers.AzureActiveDirectoryV2
{
public class AzureActiveDirectoryV2AuthenticatorConfiguration : AuthenticatorConfiguration
{
public string ClientId { get; set; }
public string ClientSecret { get; set; }
public AzureActiveDirectoryV2AuthenticatorConfiguration()
{
AuthenticationType = AzureActiveDirectoryV2Authenticator.DefaultAuthenticationType;
}
public override void ApplyToOwinSecurityOptions(AuthenticationOptions options)
{
base.ApplyToOwinSecurityOptions(options);
var openIdOptions = options as OpenIdConnectAuthenticationOptions;
if (openIdOptions != null)
{
// Set passive so that a HTTP 401 does not automatically trigger
// Azure AD authentication. NuGet uses an explicit challenge to trigger
// the auth flow.
openIdOptions.AuthenticationMode = AuthenticationMode.Passive;
// Make sure ClientId and ClientSecret is configured
if (String.IsNullOrEmpty(ClientId))
{
throw new ConfigurationErrorsException(String.Format(
CultureInfo.CurrentCulture,
ServicesStrings.MissingRequiredConfigurationValue,
"Auth.CommonAuth.ClientId"));
}
if (String.IsNullOrEmpty(ClientSecret))
{
throw new ConfigurationErrorsException(String.Format(
CultureInfo.CurrentCulture,
ServicesStrings.MissingRequiredConfigurationValue,
"Auth.CommonAuth.ClientSecret"));
}
openIdOptions.ClientId = ClientId;
openIdOptions.ClientSecret = ClientSecret;
openIdOptions.Authority = String.Format(CultureInfo.InvariantCulture, AzureActiveDirectoryV2Authenticator.Authority, AzureActiveDirectoryV2Authenticator.V2CommonTenant);
}
}
}
}