Skip to content

Commit

Permalink
Add sign in feature with file addition to context (#61)
Browse files Browse the repository at this point in the history
* Add sign in feature with file addition to context

* Put yaml back. Bump changelog

Signed-off-by: Aidan Jensen <[email protected]>

* forgot json in the python-common version

Signed-off-by: Aidan Jensen <[email protected]>

* Bump version

Signed-off-by: Aidan Jensen <[email protected]>

* Update src/auth/auth.ts

Co-authored-by: Paul Mandel <[email protected]>

* Listen for create and delete file events for config

---------

Signed-off-by: Aidan Jensen <[email protected]>
Co-authored-by: artificial-smahon <[email protected]>
Co-authored-by: Paul Mandel <[email protected]>
Co-authored-by: Shawn Mahon <[email protected]>
  • Loading branch information
4 people authored Aug 27, 2024
1 parent 40b652e commit dd68c12
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [3.2.0]

- Add Sign-In feature

## [3.1.8]

- Loading Config View
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"type": "git",
"url": "https://github.com/artificialinc/workflow-author-extension"
},
"version": "3.1.8",
"version": "3.2.0",
"icon": "resources/icon.png",
"engines": {
"vscode": "^1.71.0"
Expand Down Expand Up @@ -153,6 +153,15 @@
"category": "Artificial",
"icon": "$(refresh)"
},
{
"command": "adapterActions.signin",
"title": "Sign In",
"category": "Artificial",
"icon": {
"light": "resources/light/refresh.svg",
"dark": "resources/dark/refresh.svg"
}
},
{
"command": "adapterActions.updateAdapterImage",
"title": "Update Adapter Image",
Expand Down Expand Up @@ -279,6 +288,9 @@
{
"command": "adapterActions.executeAdapterAction",
"when": "config.artificial.workflow.author.devMode"
},
{
"command": "adapterActions.signin"
}
],
"view/title": [
Expand Down Expand Up @@ -422,4 +434,4 @@
"extensionDependencies": [
"vscode.git"
]
}
}
85 changes: 85 additions & 0 deletions src/auth/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright 2022 Artificial, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import * as vscode from 'vscode';
import { addFileToContext } from '../utils';
import { OutputLog } from '../providers/outputLogProvider';

export async function authExternalUriRegistration(context: vscode.ExtensionContext) {
// Register a URI handler for the authentication callback
vscode.window.registerUriHandler({
handleUri(uri: vscode.Uri): vscode.ProviderResult<void> {
if (uri.path === '/auth-complete') {
// Parse query parameters
const query = new URLSearchParams(uri.query);
const instanceURL = query.get('instanceURL');
const token = query.get('token');

if (instanceURL === null || token === null) {
vscode.window.showErrorMessage('Sign in failed: missing instanceURL or token');
return;
}
// Convert instanceURL into a valid host for AC
const u = new URL(instanceURL);

const generatedObj = {
artificial: {
host: u.host,
token: token,
},
};

addFileToContext(JSON.stringify(generatedObj), 'generated.yaml');

vscode.window.showInformationMessage(`Sign in successful!. Please make sure generated.yaml is added to your .gitignore file`);
} else {
vscode.window.showErrorMessage(`Sign in failed: invalid URI path ${uri.path}`);
}
}
});

// Register a sign in command
context.subscriptions.push(
vscode.commands.registerCommand(`adapterActions.signin`, async () => {
// Ask for instance url
const instanceUrl = await vscode.window.showInputBox({
prompt: 'Enter the URL of your instance',
placeHolder: 'https://example.artificial.com',
});

if (instanceUrl === undefined) {
vscode.window.showErrorMessage('Sign in failed: missing instance URL');
return;
}
try {
new URL(instanceUrl);
} catch (e) {
const log = OutputLog.getInstance();
log.log(`Error getting instance url during signin: ${e}`);
vscode.window.showErrorMessage(`Sign in failed: invalid instance URL`);
return;
}

// Get an externally addressable callback URI for the handler that the authentication provider can use
const extensionId = 'artificial.artificial-workflow-extension';
const callbackUri = await vscode.env.asExternalUri(
vscode.Uri.parse(`${instanceUrl}/app/#/vscode-login?instanceURL=${instanceUrl}&redirect=${vscode.env.uriScheme}://${extensionId}/auth-complete`)
);
vscode.env.openExternal(callbackUri);
})
);

}
12 changes: 12 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ import { DataTreeView } from './views/dataTreeView';
import { ArtificialAdapter, ArtificialAdapterManager } from './adapter/adapter';
import { Registry } from './registry/registry';
import { UnimplementedError, getRemoteScope } from './adapter/grpc/grpc';
import { authExternalUriRegistration } from './auth/auth';



export async function activate(context: vscode.ExtensionContext) {
// Setup authentication URI handler before config so it can be used to fill out config
authExternalUriRegistration(context);

// Config Setup
const { configVals, rootPath } = await setupConfig(context);
if (!rootPath) {
Expand Down Expand Up @@ -194,6 +200,12 @@ async function setupConfig(context: vscode.ExtensionContext) {
watchConfig.onDidChange((uri) => {
initConfig(rootPath);
});
watchConfig.onDidCreate((uri) => {
initConfig(rootPath);
});
watchConfig.onDidDelete((uri) => {
initConfig(rootPath);
});
context.subscriptions.push(watchConfig);
return { configVals, rootPath };
}
Expand Down
13 changes: 11 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022 Artificial, Inc.
Copyright 2022 Artificial, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -11,7 +11,7 @@ Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
*/

import * as fs from 'fs';
Expand Down Expand Up @@ -40,6 +40,15 @@ export async function initConfig(rootPath: string) {
}
}

export async function addFileToContext(file: string, filename: string) {
try {
await artificialAwaitTask('Add File to Context', `afconfig add-file ${filename} '${file}'`);
} catch {
const log = OutputLog.getInstance();
log.log('Error Adding File to Context. The artificial-common package may be outdated. v0.2.3 or newer is required.');
}
}

String.prototype.cleanQuotes = function (): string {
return this.replace(new RegExp('\'|"', 'g'), '');
};
Expand Down

0 comments on commit dd68c12

Please sign in to comment.