Install-SecureMfaApiOtpProvider.ps1

<#
     .SYNOPSIS
        Installs SecureMFA.com MFA API OTP authentication provider for ADFS.
    .DESCRIPTION
        Deployment must be done from elevated PowerShell command window
        If you are using federation server farm that uses Windows Internal Database, you must start installation using the primary federation server of the farm as a MAIN node.
        Installation needs to be executed on ADFS farm server (not web application proxy servers).
 
        Dependencies:
            * System which executes a script must have Microsoft Framework 4.6.1 and above installed.
            * SecureMfaApiOtpProvider.dll file must be present in script directory.
            * SecureMfaApiOtpProvider.json configuration file must be present in script directory.
                                     
            Bellow is a sample of valid Json config file with minimal configuration required for installation:
             {
              "company": "MyCompany",
              "serialkey": "m00000000",
              "subscriptionid": "1000000000000000000000001",
              "sqlserver": "asqlaol1.adatum.labnet,1433",
              "sqldbname": "SecureMfaOTP",
              "sqlintegratedsecurity": "true",
              "sqluseraccount": "",
              "sqluserpassword": "",
              "api_endpoint": "https://rb8zdww076.execute-api.eu-west-1.amazonaws.com/prod/",
              "api_timeout": "5000",
              "api_code_text": "SecureMFA API OTP Code:",
              "api_auth_endpoint_enabled": "false",
              "api_headers_key": "x-api-key",
              "api_headers_value": "P4WK6mUMgL6ztXtiJUurA3Fhn5Xjbejy1ZAhwokT",
              "data_encryption": "false",
              "data_encryption_passphrase": "d9GhT=7=Ox8-+LaZ",
              "ui_customization": "false",
              "ui_login_text": "",
              "ui_login_failures": "0",
              "ui_lockout_minutes": "5",
              "totp_customization": "false",
              "totp_validity_seconds": "30",
              "proxy_enable": "false",
              "proxy_server": "proxy.adatum.labnet",
              "proxy_port": "8080",
              "auth_mode": "SQL",
              "verboselog": "false"
            }
 
    .PARAMETER NotMainNode
        NotMainNode parameter is required when you do installation on multiple ADFS nodes (not web application proxy servers).
        This needs to be executed on other adfs servers when installation of a provider is done on the MAIN(First) ADFS node.
 
    .NOTES
        Version: 2.0.0.1
        Author: SecureMfa.com
        Creation Date: 29/07/2020
        Purpose/Change: Changed to subscription licensing model.
   
    .EXAMPLE
        C:\PS> Install-SecureMfaApiOtpProvider
 
        This command will install OTP authentication provider on the MAIN ADFS node.
 
    .EXAMPLE
        C:\PS> Install-SecureMfaApiOtpProvider -NotMainNode
          
        This command will install OTP authentication provider on OTHER ADFS node(s).
#>


$dllpath = (Join-Path -Path $PSScriptRoot -ChildPath SecureMfaApiOtpProvider.dll)
$dllversion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo("$dllpath").FileVersion
$configpath = (Join-Path -Path $PSScriptRoot -ChildPath SecureMfaApiOtpProvider.json)

Write-Host "File: $dllpath"
Write-Host "Version: $dllversion"
Write-Host "Configuration: $configpath"

#Check if windows events source for application log exist, if not create one.
if ([System.Diagnostics.EventLog]::SourceExists("Secure MFA API OTP") -eq $False) {New-EventLog -LogName "Application" -Source "Secure MFA API OTP" ; Write-Host "Secure MFA API OTP Log Source Created."}

#Check if ADFS service is available
if((Get-Service adfssrv -ErrorAction SilentlyContinue).Status -eq "Stopped") {Start-Service adfssrv ; write-host "Starting ADFS Service on $env:COMPUTERNAME" -ForegroundColor Yellow;}

#Load GAC Assembly
Set-location $PSScriptRoot            
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a") 
$publish = New-Object System.EnterpriseServices.Internal.Publish

Function Install-SecureMfaApiOtpProvider {
Param
(
    [Parameter(Mandatory=$false, ParameterSetName="Default")]
    [Switch]$NotMainNode
)

    #OTHER nodes install
    if ($NotMainNode) {  

    try
    {
        $Error.Clear()
        if (!(Test-Path $dllpath -Type Leaf) ) { throw "The assembly $dllpath does not exist" }

        write-host "Installing SecureMfaApiOtpProvider on the OTHER node" -ForegroundColor Cyan

        #Remove SecureMfaApiOtpProvider DLL from GAC assembly
        $publish.GacRemove($dllpath)       

        #Add SecureMfaApiOtpProvider DLL to GAC assembly
        $publish.GacInstall($dllpath)     

        #Restart ADFS service
        write-host "Restarting adfssrv service." -ForegroundColor Green
        Stop-Service adfssrv
        Start-Service adfssrv

    }
    catch
    {
        Write-Host "$($MyInvocation.InvocationName): $_" -ForegroundColor red
    }
    }

    #MAIN nodes install
    else {

    try
    {
        $Error.Clear()
        if (!(Test-Path $dllpath -Type Leaf) ) { throw "The assembly $dllpath does not exist" }               

        write-host "Installing SecureMfaApiOtpProvider on the MAIN node using configuration $configpath" -ForegroundColor Cyan
        #Remove additional authentication providers from ADFS global policy and unregister SecureMfaApiOtpProvider
        write-host "Removing additional authentication providers from ADFS global policy and unregistering SecureMfaApiOtpProvider." -ForegroundColor Green
        Set-AdfsGlobalAuthenticationPolicy -AdditionalAuthenticationProvider ""
        unregister-AdfsAuthenticationProvider -Name �SecureMfaApiOtpProvider� -Confirm:$false

        #Restart ADFS service
        write-host "Restarting adfssrv service." -ForegroundColor Green
        Stop-Service adfssrv
        Start-Service adfssrv
    
        write-host "Installing SecureMfaApiOtpProvider..." -ForegroundColor Green

        #Remove SecureMfaApiOtpProvider DLL from GAC assembly
        Write-Host "Removing SecureMfaApiOtpProvider $dllpath" -ForegroundColor yellow;
        $publish.GacRemove($dllpath)       

        #Add SecureMfaApiOtpProvider DLL to GAC assembly
        Write-Host "Adding SecureMfaApiOtpProvider $dllpath" -ForegroundColor Green;        
        $publish.GacInstall($dllpath)     

        #Register SecureMfaApiOtpProvider addapter
        $typeName = �SecureMfaApiOtpProvider.AuthenticationAdapter, SecureMfaApiOtpProvider, Version=$dllversion, Culture=neutral, PublicKeyToken=79c33deee9ac4a11, processorArchitecture=MSIL�
        Register-AdfsAuthenticationProvider -TypeName $typeName -Name �SecureMfaApiOtpProvider� -ConfigurationFilePath $configpath

        #ADFS 2019 requires update to http security headers to allow display of images for QR code to be display from external source.
        #You can update allows source with exact URL instated of any location (*).
        if (Get-Command "Get-AdfsFarmInformation" -errorAction SilentlyContinue) 
        {
        if ((Get-AdfsFarmInformation).CurrentFarmBehavior -ge 4) 
            {
            Set-AdfsResponseHeaders -SetHeaderName "Content-Security-Policy" -SetHeaderValue "default-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src * data:;" ;
            (get-AdfsResponseHeaders).ResponseHeaders | fl ;
            }
        }

        #Restart ADFS service
        write-host "Restarting adfssrv service." -ForegroundColor Green
        Stop-Service adfssrv
        Start-Service adfssrv

        #Add SecureMfaApiOtpProvider as additional authentication provider in ADFS
        write-host "Setting SecureMfaApiOtpProvider as additional authentication provider in global authentication policy." -ForegroundColor Green
        Set-AdfsGlobalAuthenticationPolicy -AdditionalAuthenticationProvider "SecureMfaApiOtpProvider"     
    
    }
    catch
    {
        Write-Host "$($MyInvocation.InvocationName): $_" -ForegroundColor red
    }
    }

    #List all authentication providers
    Get-AdfsAuthenticationProvider | select name

}