Skip to content
This repository has been archived by the owner on Nov 8, 2019. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Renamed Get-JWTToken, added first OPTICS API, prepared for auto-renew…
…al of token
  • Loading branch information
jan-tee committed Jul 26, 2018
1 parent 9d07ca1 commit 2f727fd
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 11 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
@@ -1,7 +1,16 @@
# Changelog

## v 0.6.5
* Updated function names
* Prepared auto-renewal for tokens

## v 0.6.4 (2018.07.17)
* Added support for first OPTICS APIs

## v 0.6.3 (2018.05.30)
* Get-CyAPI supports positional parameter for console selection, allowing for short-hand form "Get-CyAPI <Console>"
* Exposed some JWT primitives
* Added more -verbose support to Get-CyAPI

## v 0.6.2 (2018.05.29)
* Encapsulated the REST method call function to allow for proxy support
Expand Down
42 changes: 34 additions & 8 deletions CyAPI.ps1
Expand Up @@ -19,6 +19,10 @@
Class CylanceAPIHandle {
[string]$AccessToken
[string]$BaseUrl
[securestring]$APISecret
[string]$APIId
[string]$APITenantId
[datetime]$ExpirationTime
}

Class CylanceGlobalSettings {
Expand Down Expand Up @@ -85,6 +89,7 @@ function Get-CyAPI {
}

Begin {
$expirationTimeout = 1800
switch ($PSCmdlet.ParameterSetName)
{
"Direct"
Expand All @@ -96,9 +101,9 @@ function Get-CyAPI {

$claims = @{}

$jwtBearerToken = Get-JWTToken `
$jwtBearerToken = Get-CyJWTToken `
-claims $claims `
-expirationSeconds 1800 `
-expirationSeconds $expirationTimeout `
-secret $pw `
-iss "http://cylance.com" `
-tid $APITenantId `
Expand All @@ -114,7 +119,8 @@ function Get-CyAPI {
}

try {
$result = Invoke-CyRestMethod @rest
Write-Verbose "Requesting auth for JWT token: $($jwtBearerToken)"
$result = Invoke-CyRestMethod @rest -DoNotRenewToken
}
catch {
Write-Error $_.Exception
Expand All @@ -135,6 +141,10 @@ function Get-CyAPI {
[CylanceAPIHandle]$r = New-Object CylanceAPIHandle
$r.AccessToken = $result.access_token
$r.BaseUrl = $baseUrl
$r.APISecret = $APISecret
$r.APIId = $APIId
$r.APITenantId = $APITenantId
$r.ExpirationTime = (Get-Date).AddSeconds($expirationTimeout - 1740)

if ($Scope -eq "Session") {
$script:GlobalCyAPIHandle = $r
Expand Down Expand Up @@ -377,9 +387,27 @@ function Invoke-CyRestMethod {
[parameter(Mandatory=$false)]
[string]$OutFile = $null,
[parameter(Mandatory=$false)]
[Switch]$ProxyUseDefaultCredentials
[Switch]$ProxyUseDefaultCredentials,
[Switch]$DoNotRenewToken
)

if (((Get-Date) -gt $API.ExpirationTime) -and -not $DoNotRenewToken) {
# renew token automatically
Write-Verbose "Renewing token: $($DoNotRenewToken); previous token: $($API | out-string)"

$APIrenewed = Get-CyAPI `
-Scope None `
-APIId $API.APIId `
-APITenantId $API.APITenantId `
-APISecret $API.APISecret `
-APIAuthUrl $API.BaseUrl

Write-Verbose "Renewing token $($APIrenewed | out-string)"

$API.ExpirationTime = $APIrenewed.ExpirationTime
$API.AccessToken = $APIrenewed.AccessToken
}

$rest = @{
Method = $Method
Uri = $Uri
Expand Down Expand Up @@ -423,9 +451,7 @@ function Invoke-CyRestMethod {
}
}

if ($Verbose) {
$ht = $rest | Out-String
Write-Verbose "Invoking CyREST method using params: $($ht)"
}
Write-Verbose "Invoking CyREST method using params: $($rest | Out-String)"

Invoke-RestMethod @rest
}
Binary file modified CyCLI.psd1
Binary file not shown.
Binary file modified CyCLI.psm1
Binary file not shown.
4 changes: 2 additions & 2 deletions CyCrypto.ps1
Expand Up @@ -20,7 +20,7 @@ function Get-HMACSHA256 {
Can accept additional claims to include in request.
#>
function Get-JWTToken {
function Get-CyJWTToken {
Param (
[parameter(Mandatory=$True)]
[Hashtable]$claims = @{},
Expand Down Expand Up @@ -76,7 +76,7 @@ function Get-JWTToken {
"${h}.${p}.${s}"
}

function Get-ClaimsFromJwtToken {
function Get-CyClaimsFromJwtToken {
Param (
[parameter(Mandatory=$true)]
[String]$token
Expand Down
15 changes: 15 additions & 0 deletions CyOptics.ps1
@@ -0,0 +1,15 @@
<#
.SYNOPSIS
Gets a list of all detections from the console.
.PARAMETER API
Optional. API Handle (use only when not using session scope).
#>
function Get-CyDetectionList {
Param (
[parameter(Mandatory=$false)]
[CylanceAPIHandle]$API = $GlobalCyAPIHandle
)

Read-CyData -API $API -Uri "$($API.BaseUrl)/detections/v2"
}
3 changes: 2 additions & 1 deletion Invoke-InstallModule.ps1
Expand Up @@ -30,7 +30,8 @@ Begin {
'CyPolicies.ps1',
'CyInstallers.ps1',
'CyGlobalLists.ps1',
'CyUsers.ps1'
'CyUsers.ps1',
'CyOptics.ps1'
)
}
Catch {
Expand Down

0 comments on commit 2f727fd

Please sign in to comment.