Skip to content

Commit

Permalink
Basic auth flow
Browse files Browse the repository at this point in the history
  • Loading branch information
gbraad committed Jun 7, 2023
1 parent efd644e commit 24c6e62
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as podmanDesktopAPI from '@podman-desktop/api';
import { exec } from './util';
import { BackendState, StatusResponse, TailscaleUpResponse } from './types'

const containerName = 'tailscale-system';
const containerImage = 'ghcr.io/spotsnel/tailscale-systemd:latest';
Expand Down Expand Up @@ -34,14 +35,20 @@ export async function activate(extensionContext: podmanDesktopAPI.ExtensionConte
}
}

const status = await (await exec('podman', ['exec', containerName, 'tailscale', 'status', '--json'])).stdOut;
// check registration status
console.log(status);
const statusResponse = await getTailscaleStatus()
const [status, rawStatus] = statusResponse

// check registration status
// if not registered => "BackendState": "NeedsLogin"
// register
if (status.BackendState === "NeedsLogin") {
// no markdown description
//const upResponse = await getTailscaleUp()

// else, get status
await podmanDesktopAPI.window.showInformationMessage(
'Please register node to your tailnet\n\n' + status.AuthURL,
'OK',
);
}

// set statusbar
//const item = podmanDesktopAPI.window.createStatusBarItem(podmanDesktopAPI.StatusBarAlignRight, 100);
Expand All @@ -53,3 +60,13 @@ export async function activate(extensionContext: podmanDesktopAPI.ExtensionConte
export function deactivate(): void {
console.log('Deactivating Tailscale extension');
}

async function getTailscaleStatus(): Promise<[StatusResponse, string]> {
const status = await (await exec('podman', ['exec', containerName, 'tailscale status --json']))
return [JSON.parse(status.stdOut), status.stdOut]
}

async function getTailscaleUp(): Promise<[TailscaleUpResponse, string]> {
const up = await (await exec('podman', ['exec', containerName, 'tailscale up --reset --force-reauth --json']))
return [JSON.parse(up.stdOut), up.stdOut]
}
44 changes: 44 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// BackendState
// Keep in sync with https://github.com/tailscale/tailscale/blob/main/ipn/backend.go
export type BackendState =
| "NoState"
| "NeedsMachineAuth"
| "NeedsLogin"
| "InUseOtherUser"
| "Stopped"
| "Starting"
| "Running"

export type StatusResponse = {
BackendState: BackendState
AuthURL: string
Self: {
ID: string
UserID: number
HostName: string
DNSName: string
OS: string
TailscaleIPs: string[]
Capabilities: string[]
}
User: Record<string, TailscaleUser> | null
CurrentTailnet: {
Name: string
MagicDNSSuffix: string
MagicDNSEnabled: boolean
} | null
}

export type TailscaleUser = {
ID: number
LoginName: string
DisplayName: string
ProfilePicURL: string
Roles: string[]
}

export type TailscaleUpResponse = {
BackendState: BackendState
AuthURL?: string // e.g. https://login.tailscale.com/a/0123456789abcdef
QR?: string // a DataURL-encoded QR code PNG of the AuthURL
}

0 comments on commit 24c6e62

Please sign in to comment.