-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathIEnvExtensions.cs
More file actions
63 lines (56 loc) · 2.33 KB
/
IEnvExtensions.cs
File metadata and controls
63 lines (56 loc) · 2.33 KB
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
61
62
63
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace Microsoft.Authentication.AzureAuth
{
using Microsoft.Authentication.MSALWrapper;
using Microsoft.Office.Lasso.Interfaces;
using Microsoft.Office.Lasso.Telemetry;
using System.Collections.Generic;
using System;
/// <summary>
/// Extension methods to Lasso's <see cref="IEnv"/> interface.
/// </summary>
public static class IEnvExtensions
{
private const string CorextPositiveValue = "1";
/// <summary>
/// Determines whether interactive auth is disabled or not.
/// </summary>
/// <param name="env">The <see cref="IEnv"/> to use to get environment variables.</param>
/// <returns>A boolean to indicate if interactive auth modes should be disabled.</returns>
public static bool InteractiveAuthDisabled(this IEnv env)
{
return !string.IsNullOrEmpty(env.Get(EnvVars.NoUser)) ||
string.Equals(CorextPositiveValue, env.Get(EnvVars.CorextNonInteractive));
}
/// <summary>
/// Get the auth modes from the environment or set the default.
/// </summary>
/// <param name="env">The <see cref="IEnv"/> to use.</param>
/// <param name="eventData">Event data to add the auth mode to.</param>
/// <returns>AuthModes.</returns>
public static IEnumerable<AuthMode> ReadAuthModeFromEnvOrSetDefault(this IEnv env)
{
var authModesFromEnv = env.Get(EnvVars.AuthMode);
// If auth modes are not specified in the environment, then return the default.
if (string.IsNullOrEmpty(authModesFromEnv))
{
return new[] { AuthMode.Default };
}
var result = new List<AuthMode>();
foreach (var val in authModesFromEnv.Split(','))
{
if (Enum.TryParse<AuthMode>(val, ignoreCase: true, out var mode))
{
result.Add(mode);
}
else
{
// If the environment variable is not a valid auth mode, then return an empty list.
return new List<AuthMode>();
}
}
return result;
}
}
}