Skip to content

Commit 6bc7a36

Browse files
Add Domeneshop context auth flow
Store Domeneshop API credentials with Context and add a first authenticated domains command with a shared request helper. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 37f682e commit 6bc7a36

7 files changed

Lines changed: 219 additions & 0 deletions
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function Get-DomeneshopConfig {
2+
[CmdletBinding()]
3+
param()
4+
5+
$vault = 'Domeneshop'
6+
$id = '__Domeneshop.Config'
7+
$config = Get-Context -ID $id -Vault $vault -ErrorAction SilentlyContinue
8+
9+
if (-not $config) {
10+
$config = [ordered]@{
11+
DefaultContext = $null
12+
ApiBaseUri = 'https://api.domeneshop.no/v0'
13+
}
14+
Set-Context -ID $id -Vault $vault -Context $config
15+
$config = Get-Context -ID $id -Vault $vault
16+
}
17+
18+
$config
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function Resolve-DomeneshopContext {
2+
[CmdletBinding()]
3+
param(
4+
[Parameter()]
5+
[string] $Context
6+
)
7+
8+
$resolvedContext = if ($Context) {
9+
Get-DomeneshopContext -Context $Context
10+
} else {
11+
Get-DomeneshopContext
12+
}
13+
14+
if (-not $resolvedContext) {
15+
throw "No Domeneshop context found. Run 'Connect-DomeneshopAccount' first."
16+
}
17+
18+
if ([string]::IsNullOrEmpty($resolvedContext.Token) -or -not ($resolvedContext.Secret -is [securestring])) {
19+
throw "The Domeneshop context [$($resolvedContext.ID)] is missing valid credentials."
20+
}
21+
22+
$resolvedContext
23+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function Set-DomeneshopDefaultContext {
2+
[CmdletBinding()]
3+
param(
4+
[Parameter(Mandatory)]
5+
[string] $Context
6+
)
7+
8+
$config = Get-DomeneshopConfig
9+
$config.DefaultContext = $Context
10+
Set-Context -ID '__Domeneshop.Config' -Vault 'Domeneshop' -Context $config
11+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function Invoke-DomeneshopApiRequest {
2+
[CmdletBinding()]
3+
param(
4+
[Parameter(Mandatory)]
5+
[ValidateSet('Get', 'Post', 'Put', 'Delete')]
6+
[string] $Method,
7+
8+
[Parameter(Mandatory)]
9+
[string] $Uri,
10+
11+
[Parameter(Mandatory)]
12+
[object] $Context,
13+
14+
[Parameter()]
15+
[AllowNull()]
16+
[object] $Body
17+
)
18+
19+
$credential = [pscredential]::new(
20+
[string] $Context.Token,
21+
[securestring] $Context.Secret
22+
)
23+
24+
$params = @{
25+
Method = $Method
26+
Uri = $Uri
27+
Authentication = 'Basic'
28+
Credential = $credential
29+
}
30+
31+
if ($PSBoundParameters.ContainsKey('Body')) {
32+
$params['ContentType'] = 'application/json'
33+
$params['Body'] = ($Body | ConvertTo-Json -Depth 100)
34+
}
35+
36+
Invoke-RestMethod @params
37+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
function Connect-DomeneshopAccount {
2+
<#
3+
.SYNOPSIS
4+
Stores Domeneshop API credentials in a secure context.
5+
6+
.DESCRIPTION
7+
Stores Domeneshop API credentials using the Context module and optionally sets the context as default.
8+
9+
.EXAMPLE
10+
Connect-DomeneshopAccount -Token 'my-token' -Secret (Read-Host -AsSecureString)
11+
#>
12+
[OutputType([object])]
13+
[CmdletBinding()]
14+
param(
15+
[Parameter(Mandatory)]
16+
[string] $Token,
17+
18+
[Parameter(Mandatory)]
19+
[object] $Secret,
20+
21+
[Parameter()]
22+
[string] $Context = 'default',
23+
24+
[Parameter()]
25+
[switch] $Default,
26+
27+
[Parameter()]
28+
[switch] $PassThru
29+
)
30+
31+
$secureSecret = switch ($Secret) {
32+
{ $_ -is [securestring] } { $_; break }
33+
{ $_ -is [string] } { ConvertTo-SecureString -AsPlainText $Secret -Force; break }
34+
default { throw 'Secret must be a SecureString or String value.' }
35+
}
36+
37+
$contextObject = [ordered]@{
38+
Name = $Context
39+
Token = $Token
40+
Secret = $secureSecret
41+
AuthType = 'BasicAuth'
42+
ApiBaseUri = 'https://api.domeneshop.no/v0'
43+
ConnectedAt = Get-Date
44+
}
45+
46+
Set-Context -ID $Context -Vault 'Domeneshop' -Context $contextObject
47+
48+
$config = Get-DomeneshopConfig
49+
if ($Default -or [string]::IsNullOrEmpty($config.DefaultContext)) {
50+
Set-DomeneshopDefaultContext -Context $Context
51+
}
52+
53+
if ($PassThru) {
54+
Get-DomeneshopContext -Context $Context
55+
}
56+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function Get-DomeneshopContext {
2+
<#
3+
.SYNOPSIS
4+
Gets stored Domeneshop contexts.
5+
6+
.DESCRIPTION
7+
Returns one or more contexts stored in the Domeneshop context vault.
8+
9+
.EXAMPLE
10+
Get-DomeneshopContext
11+
#>
12+
[OutputType([object])]
13+
[CmdletBinding(DefaultParameterSetName = 'GetDefault')]
14+
param(
15+
[Parameter(Mandatory, ParameterSetName = 'GetNamed')]
16+
[string] $Context,
17+
18+
[Parameter(Mandatory, ParameterSetName = 'ListAvailable')]
19+
[switch] $ListAvailable
20+
)
21+
22+
$id = switch ($PSCmdlet.ParameterSetName) {
23+
'GetNamed' { $Context; break }
24+
'ListAvailable' { '*'; break }
25+
default {
26+
$config = Get-DomeneshopConfig
27+
if ([string]::IsNullOrEmpty($config.DefaultContext)) {
28+
throw "No default Domeneshop context found. Run 'Connect-DomeneshopAccount' first."
29+
}
30+
$config.DefaultContext
31+
}
32+
}
33+
34+
Get-Context -ID $id -Vault 'Domeneshop' | Where-Object { $_.ID -ne '__Domeneshop.Config' } | Sort-Object -Property ID
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function Get-DomeneshopDomain {
2+
<#
3+
.SYNOPSIS
4+
Lists Domeneshop domains.
5+
6+
.DESCRIPTION
7+
Uses the stored Domeneshop context credentials and calls the /domains endpoint.
8+
9+
.EXAMPLE
10+
Get-DomeneshopDomain
11+
12+
.EXAMPLE
13+
Get-DomeneshopDomain -Domain '.no'
14+
#>
15+
[OutputType([object[]])]
16+
[CmdletBinding()]
17+
param(
18+
[Parameter()]
19+
[string] $Domain,
20+
21+
[Parameter()]
22+
[string] $Context
23+
)
24+
25+
$resolvedContext = Resolve-DomeneshopContext -Context $Context
26+
$apiBaseUri = [string] $resolvedContext.ApiBaseUri
27+
if ([string]::IsNullOrEmpty($apiBaseUri)) {
28+
$apiBaseUri = 'https://api.domeneshop.no/v0'
29+
}
30+
31+
$uri = "$apiBaseUri/domains"
32+
if ($Domain) {
33+
$encodedDomain = [uri]::EscapeDataString($Domain)
34+
$uri = "${uri}?domain=$encodedDomain"
35+
}
36+
37+
Invoke-DomeneshopApiRequest -Method Get -Uri $uri -Context $resolvedContext
38+
}

0 commit comments

Comments
 (0)