Get-GreenRadiusTemporaryToken.ps1

function Get-GreenRadiusTemporaryToken {
    <#
        .SYNOPSIS
            Gets information on a temp token assigned to a user.
 
        .DESCRIPTION
            Utilizes the GreenRADIUS API to retrieve information on a temp token assigned to a user.
            Relies on the user already having such a token assigned.
 
        .OUTPUTS
            $true if successful, $false if not. If not successful, an explanation of the failure
            will also be passed to Write-Error.
 
        .PARAMETER Username
            The username of the user who has the token assigned to them. This is in username@domain
            format. This does NOT necessarily use the UPN of the user, but rather the actual domain
            name as configured in GreenRADIUS, independent of the individual user.
 
        .PARAMETER RawRequest
            Variable name, without the dollar sign, in which to store the raw request which was
            created and sent to the API. This does not need to be a pre-existing variable. Mostly
            used for debugging this function.
 
        .PARAMETER RawResponse
            Variable name, without the dollar sign, in which to store the raw response from the API.
            This does not need to be a pre-existing variable. Mostly used for debugging this
            function.
 
        .NOTES
            Version 2026.02.09.1446
    #>


    <# LICENSE
 
    Copyright (c) 2026 Derek Howard and the City of Eureka, California
 
    Permission is hereby granted, free of charge, to any person obtaining a copy of this software
    and associated documentation files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use, copy, modify, merge, publish,
    and/or distribute copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
 
    The above copyright notice and this permission notice shall be included in all copies or
    substantial portions of the Software, and the original author shall be credited as having
    created the original work.
 
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
    BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    #>


    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory=$true)]
        [string]$Username,

        [string]$RawRequest,

        [string]$RawResponse
    )

    if (-not $global:GreenRadiusApiSession) {
        Write-Error "You forgot to run Connect-GreenRadiusApi!"
        return $false;
    }

    $Body = @{
        usernames = @($Username)
    }

    $Params = @{
        Uri                  = "https://$($global:GreenRadiusApiSession.HostName)/gras-api/v2/mgmt/temporary-tokens"
        Method               = "Get"
        Credential           = $global:GreenRadiusApiSession.Credential
        Body                 = ($Body | ConvertTo-Json -Depth 5)
        ContentType          = "application/json"
    }

    if ($RawRequest) {
        Set-Variable -Name $RawRequest -Value $apiResult -Scope 1
    }

    $apiResult = Invoke-RestMethod @Params
    if ($apiResult.GetType().Name -eq "String") {
        #This only seems necessary for the /tokenassignment api (used in Get-GreenRadiusTokenAssignments)
        #and even then only when no search parameters are specified, but here we go anyway
        $apiResult = $apiResult | ConvertFrom-Json -AsHashTable
    }
    if ($RawResponse) {
        Set-Variable -Name $RawResponse -Value $apiResult -Scope 1
    }

    if ($apiResult.records_found.Count -gt 0) {
        $remainingUses = $apiResult.records_found.records.$Username.count_of_max_auth
        $expirationDate = $(([System.DateTimeOffset]::FromUnixTimeSeconds($apiResult.records_found.records.$Username.expiry_date)).LocalDateTime)
        Write-Host "User $Username has a temporary token with $remainingUses remaining uses and an expiration time of $expirationDate."
        return $true
    } else {
        if ($apiResult.records_skipped.Count -gt 0) {
            Write-Error $apiResult.records_skipped.records.$Username.description
        } elseif ($apiResult.records_invalid.Count -gt 0) {
            Write-Error $apiResult.records_invalid.records.$Username.description
        }
        return $false
    }
}