-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from gemini-testing/base.implementation
feat: plugin base implementation
- Loading branch information
Showing
6 changed files
with
165 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,29 @@ | ||
# hermione-oauth | ||
|
||
Plugin for convenient setting of authorization header with OAuth token | ||
Some Remote WebDriver Servers requires OAuth authorization for each request. This plugin is useful for setting of authorization header with OAuth token. | ||
|
||
## Install | ||
|
||
```bash | ||
npm install hermione-oauth --save-dev | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
// .hermione.conf.js | ||
module.exports = { | ||
// ... | ||
plugins: { | ||
"hermione-oauth": { | ||
enabled: true, // plugin is enabled by default | ||
token: "<token>", // option also accepts absolute filepath with a token | ||
help: "https://...", // information on where to get a token | ||
}, | ||
}, | ||
}; | ||
``` | ||
|
||
Each plugin option can be redefined by | ||
- environment variable which starts with prefix `hermione_oauth_`, for example, `hermione_oauth_token=123-456-789` | ||
- CLI option which starts with prefix `--oauth-`, for example, `--oauth-token=123-456-789` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { option, root, section } from "gemini-configparser"; | ||
import type { Parser } from "gemini-configparser"; | ||
|
||
import { PluginOptionTypeError, PluginTokenOptionAbsenceError } from "./errors"; | ||
|
||
export type PluginConfig = { | ||
enabled: boolean; | ||
token: string; | ||
help: string; | ||
}; | ||
|
||
const isNonEmptyString = (v: unknown): boolean => typeof v === "string" && v !== ""; | ||
|
||
const assertType = <T>(name: string, validate: (v: unknown) => boolean, type: string) => { | ||
return (v: T) => { | ||
if (!validate(v)) { | ||
throw new PluginOptionTypeError(name, type); | ||
} | ||
}; | ||
}; | ||
|
||
const boolean = (name: string): Parser<boolean> => | ||
option({ | ||
parseEnv: v => Boolean(JSON.parse(v)), | ||
parseCli: v => Boolean(JSON.parse(v)), | ||
defaultValue: true, | ||
validate: assertType(name, v => typeof v === "boolean", "boolean"), | ||
}); | ||
|
||
const nonEmptyString = (name: string): Parser<string> => | ||
option({ | ||
defaultValue: "", | ||
validate: assertType(name, isNonEmptyString, "non empty string"), | ||
}); | ||
|
||
export function parseConfig(options: Record<string, unknown>): PluginConfig { | ||
const { env, argv } = process; | ||
|
||
const parseOptions = root<PluginConfig>( | ||
section({ | ||
enabled: boolean("enabled"), | ||
token: option({ | ||
defaultValue: "", | ||
validate: (v, config) => { | ||
if (!isNonEmptyString(v)) { | ||
throw new PluginTokenOptionAbsenceError(config.help); | ||
} | ||
}, | ||
}), | ||
help: nonEmptyString("help"), | ||
}), | ||
{ envPrefix: "hermione_oauth_", cliPrefix: "--oauth-" }, | ||
); | ||
|
||
return parseOptions({ options, env, argv }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class PluginError extends Error { | ||
constructor(message: string) { | ||
super(`hermione-oauth: ${message}`); | ||
} | ||
} | ||
|
||
export class PluginOptionTypeError extends PluginError { | ||
constructor(name: string, type: string) { | ||
super(`'${name}' option must be of a ${type} type`); | ||
} | ||
} | ||
|
||
export class PluginTokenError extends PluginError { | ||
constructor(message: string, help: string) { | ||
super(`${message}, see ${help} to get it`); | ||
} | ||
} | ||
|
||
export class PluginTokenReadError extends PluginTokenError { | ||
constructor(path: string, help: string) { | ||
super(`unable to read token from file ${path}`, help); | ||
} | ||
} | ||
|
||
export class PluginTokenOptionAbsenceError extends PluginTokenError { | ||
constructor(help: string) { | ||
super("'token' option must be of a non empty string type", help); | ||
} | ||
} | ||
|
||
export class PluginTokenAbsenceError extends PluginTokenError { | ||
constructor(filepath: string, help: string) { | ||
super(`token is absence at file ${filepath}`, help); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import path from "path"; | ||
|
||
import type Hermione from "hermione"; | ||
|
||
import { parseConfig } from "./config"; | ||
import readToken from "./read-token"; | ||
|
||
export = (hermione: Hermione, options: Record<string, unknown>): void => { | ||
const config = parseConfig(options); | ||
|
||
if (!config.enabled) { | ||
return; | ||
} | ||
|
||
const token = path.isAbsolute(config.token) ? readToken(config.token, config.help) : config.token; | ||
|
||
hermione.on(hermione.events.BEGIN, () => { | ||
hermione.config.getBrowserIds().forEach(browserId => { | ||
const browserConfig = hermione.config.forBrowser(browserId); | ||
|
||
browserConfig.headers = { ...browserConfig.headers, Authorization: `OAuth ${token}` }; | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import fs from "fs"; | ||
|
||
import { PluginTokenReadError, PluginTokenAbsenceError } from "./errors"; | ||
|
||
export default function (filepath: string, help: string): string { | ||
let token: string; | ||
|
||
try { | ||
token = fs.readFileSync(filepath, { encoding: "utf-8" }).trim(); | ||
} catch (e) { | ||
throw new PluginTokenReadError(filepath, help); | ||
} | ||
|
||
if (token === "") { | ||
throw new PluginTokenAbsenceError(filepath, help); | ||
} | ||
|
||
return token; | ||
} |