Skip to content

Instantly share code, notes, and snippets.

@Chirishman
Last active December 21, 2017 04:43
Show Gist options
  • Save Chirishman/91870e9740993583576dfbcb5d21c95f to your computer and use it in GitHub Desktop.
Save Chirishman/91870e9740993583576dfbcb5d21c95f to your computer and use it in GitHub Desktop.
Test dashboard - Auth with LM or AD
function Get-ADGroupMembership {
Param(
[Parameter(
Position = 0,
Mandatory = $true,
ValueFromPipelineByPropertyName = $true
)]
[Alias('UserName','UN')]
[String]
$User
)
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$AuthLookup = @{
$true = 'machine'
$false = 'domain'
}
$DS = [System.DirectoryServices.AccountManagement.PrincipalContext]::new($AuthLookup[($env:USERDOMAIN -eq $env:COMPUTERNAME)])
$UserObject = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($DS, $User)
$Groups = $UserObject.GetAuthorizationGroups()
$Groups | select -ExpandProperty Name
}
function Test-ADCredential {
Param(
[Parameter(
Position=0,
Mandatory=$true,
ValueFromPipeline = $true)]
[pscredential]$Credential
)
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$AuthLookup = @{
$true = 'machine'
$false = 'domain'
}
$DS = [System.DirectoryServices.AccountManagement.PrincipalContext]::new($AuthLookup[($env:USERDOMAIN -eq $env:COMPUTERNAME)])
$Credential.GetNetworkCredential() | % {
$DS.ValidateCredentials($_.UserName, $_.Password)
}
}
function Test-ADGroupMembership {
Param(
[Parameter(
Position = 0,
Mandatory = $true,
ValueFromPipelineByPropertyName = $true
)]
[Alias('UserName','UN')]
[String]
$User,
[Parameter(
Position=1,
Mandatory=$true,
ValueFromPipelineByPropertyName = $true
)]
[string]$TargetGroup
)
$Groups = Get-ADGroupMembership -User $User
$TargetGroup -in $Groups
}
Import-Module UniversalDashboard
$FormLogin = New-UDAuthenticationMethod -Endpoint {
param([PSCredential]$Credentials)
$AuthorizedGroup = 'Users'
. ".\Test-ADGroupMembership.ps1"
. ".\Test-ADCredential.ps1"
. ".\Get-ADGroupMembership.ps1"
if ($Credentials | ? {$_ | Test-ADCredential} | Test-ADGroupMembership -TargetGroup $AuthorizedGroup) {
New-UDAuthenticationResult -Success -UserName $Credentials.UserName
}
New-UDAuthenticationResult -ErrorMessage "You aren't $($Credentials.UserName)!!!"
}
$LoginPage = New-UDLoginPage -AuthenticationMethod @($FormLogin)
$MyDashboardPage = New-UDPage -Url "/myDashboard" -Endpoint {
New-UDCard -Title "Welcome, $User" -Text "This is your custom dashboard."
}
$Dashboard = New-UDDashboard -LoginPage $LoginPage -Page @(
$MyDashboardPage
)
Start-UDDashboard -Dashboard $Dashboard -Port 8080 -Name 'TestDashboard'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment