Skip to content

Commit

Permalink
Make the logo configurable for browser login (#1306)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins authored Aug 21, 2023
1 parent cf276c6 commit 0027a58
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 22 deletions.
20 changes: 20 additions & 0 deletions config-defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,23 @@ experimental:

# Enable all experiments.
all_experiments: false

# Optional styling for browser login web page.
# The "css" string will be added as a stylesheet.
# The "body" string will replace the whole body content.
# Two template parameters are available:
# - {{title}} is replaced with a plain-text page title
# - {{content}} is replaced with the rest of the content HTML
browser_login:
# css: ''
body: |
<img
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkAQMAAABKLAcXAAAABlBMVEUAAADg4ODy8Xj7AAAAAXRSTlMAQObYZgAAAB5JREFUOMtj+I8EPozyRnlU4w1NMJhCcDT+hm2MAQAJBMb6YxK/8wAAAABJRU5ErkJggg=="
alt=""
width="100"
height="100"
class="icon">
<h1>{{title}}</h1>
{{content}}
57 changes: 35 additions & 22 deletions resources/oauth-listener/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct() {
$this->prompt = $_ENV['CLI_OAUTH_PROMPT'];
$this->codeChallenge = $_ENV['CLI_OAUTH_CODE_CHALLENGE'];
$this->scope = isset($_ENV['CLI_OAUTH_SCOPE']) ? $_ENV['CLI_OAUTH_SCOPE'] : '';
$this->localUrl = $localUrl = 'http://127.0.0.1:' . $_SERVER['SERVER_PORT'];
$this->localUrl = 'http://127.0.0.1:' . $_SERVER['SERVER_PORT'];
$this->response = new Response();
}

Expand Down Expand Up @@ -81,8 +81,8 @@ public function run()

// Show the final result page.
if (array_key_exists('done', $_GET)) {
$this->response->content = '<h1>Successfully logged in</h1>'
. '<p>You can return to the command line</p>';
$this->response->title = 'Successfully logged in';
$this->response->content = '<p>You can return to the command line</p>';

return;
}
Expand Down Expand Up @@ -138,15 +138,15 @@ private function sendToTerminal(array $response)
private function reportError($message = null, $error = null, $hint = null)
{
$this->response->headers['Status'] = 401;
$this->response->content = '<h1 class="error">Error</h1>';
$this->response->title = 'Error';
if (isset($error)) {
$this->response->content .= '<p class="error"><code>' . htmlspecialchars($error) . '</code></p>';
}
if (isset($message)) {
$this->response->content .= '<p class="error">' . htmlspecialchars($message) . '</p>';
}
if (isset($hint)) {
$this->response->content .= '<p class="error">' . htmlspecialchars($hint) . '</p>';
$this->response->content .= '<p class="error error-hint">' . htmlspecialchars($hint) . '</p>';
}
if ($message || $error || $hint) {
$response = ['error' => $error, 'error_description' => $message, 'error_hint' => $hint];
Expand All @@ -162,26 +162,41 @@ class Response
{
public $headers = [];
public $code = 200;
public $headTitle = '';
public $title = '';
public $content = '';

public function __construct()
{
// Set default title and headers.
$appName = getenv('CLI_OAUTH_APP_NAME') ?: 'CLI';
$this->title = htmlspecialchars($appName) . ': Authentication (temporary URL)';
$this->headTitle = htmlspecialchars($appName) . ': Authentication (temporary URL)';
$this->headers = [
'Cache-Control' => 'no-cache',
'Content-Type' => 'text/html; charset=utf-8',
];
}
}

$configJson = file_get_contents('config.json');
if ($configJson === false) {
throw new \RuntimeException('Failed to load configuration file: config.json');
}
$config = (array) json_decode($configJson, true);

$listener = new Listener();
$listener->run();

$response = $listener->getResponse();

if (!empty($config['body'])) {
$body = preg_replace_callback('/\{\{\s*(content|title)\s*}}/', function (array $matches) use ($response) {
return ['content' => $response->content, 'title' => $response->title][$matches[1]];
}, $config['body']);
} else {
$body = '<h1>' . $response->title . '</h1>' . $response->content;
}

http_response_code($response->code);
foreach ($response->headers as $name => $value) {
header($name . ': ' . $value);
Expand All @@ -191,26 +206,26 @@ public function __construct()
<html lang="en">
<head>
<meta charset="utf-8">
<title><?php echo $response->title; ?></title>
<title><?php echo $response->headTitle; ?></title>
<style>
html {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-weight: 300;
background-color: #eee;
}

h1 {
font-weight: 100;
}

body {
margin: 3em;
text-align: center;
}

img {
img.icon {
display: block;
margin: 10px auto;
margin: 3em auto 1em;
}

h1 {
font-weight: 100;
margin: 1em auto;
}

.error {
Expand All @@ -220,15 +235,13 @@ public function __construct()
font-style: oblique;
}
</style>
<?php if (!empty($config['css'])): ?>
<style>
<?php echo $config['css']; ?>
</style>
<?php endif; ?>
</head>
<body>
<img
src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkAQMAAABKLAcXAAAABlBMVEUAAADg4ODy8Xj7AAAAAXRSTlMAQObYZgAAAB5JREFUOMtj+I8EPozyRnlU4w1NMJhCcDT+hm2MAQAJBMb6YxK/8wAAAABJRU5ErkJggg=="
alt=""
width="100"
height="100">

<?php echo $response->content; ?>

<?php echo $body; ?>
</body>
</html>
3 changes: 3 additions & 0 deletions src/Command/Auth/BrowserLoginCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ private function createDocumentRoot($dir)
if (!file_put_contents($dir . '/index.php', file_get_contents(CLI_ROOT . '/resources/oauth-listener/index.php'))) {
throw new \RuntimeException('Failed to write temporary file: ' . $dir . '/index.php');
}
if (!file_put_contents($dir . '/config.json', json_encode($this->config()->getWithDefault('browser_login', []), JSON_UNESCAPED_SLASHES))) {
throw new \RuntimeException('Failed to write temporary file: ' . $dir . '/config.json');
}
}

/**
Expand Down

0 comments on commit 0027a58

Please sign in to comment.