-
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 #10 from agiledigital-labs/feature/IE-8
Feature/ie 8
- Loading branch information
Showing
4 changed files
with
169 additions
and
54 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
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,80 @@ | ||
import { Argv } from 'yargs'; | ||
import { RootCommand } from '..'; | ||
|
||
import { | ||
oktaManageClient, | ||
OktaConfiguration, | ||
user, | ||
User, | ||
} from './services/user-service'; | ||
|
||
const deactivateUser = async ( | ||
oktaConfiguration: OktaConfiguration, | ||
userId: string | ||
): Promise<User> => { | ||
const client = oktaManageClient(oktaConfiguration); | ||
|
||
const oktaUser = await client.getUser(userId); | ||
|
||
// eslint-disable-next-line functional/no-expression-statement | ||
await oktaUser.deactivate(); | ||
|
||
const decativatedOktaUser = await client.getUser(userId); | ||
|
||
return user(decativatedOktaUser); | ||
}; | ||
|
||
export default ( | ||
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types | ||
rootCommand: RootCommand | ||
): Argv<{ | ||
readonly clientId: string; | ||
readonly privateKey: string; | ||
readonly organisationUrl: string; | ||
readonly userId: string; | ||
}> => | ||
rootCommand.command( | ||
'deactivate-user [user-id]', | ||
'Deactivates the specified user', | ||
// eslint-disable-next-line functional/no-return-void, @typescript-eslint/prefer-readonly-parameter-types | ||
(yargs) => { | ||
// eslint-disable-next-line functional/no-expression-statement | ||
yargs.positional('user-id', { | ||
describe: 'a unique identifier for the server', | ||
type: 'string', | ||
demandOption: true, | ||
}); | ||
}, | ||
async (args: { | ||
readonly clientId: string; | ||
readonly privateKey: string; | ||
readonly organisationUrl: string; | ||
readonly userId: string; | ||
}) => { | ||
// eslint-disable-next-line functional/no-try-statement | ||
try { | ||
const user = await deactivateUser( | ||
{ | ||
...args, | ||
}, | ||
args.userId | ||
); | ||
// eslint-disable-next-line functional/no-expression-statement | ||
console.info(user); | ||
} catch (error: unknown) { | ||
// eslint-disable-next-line functional/no-throw-statement | ||
throw error instanceof Error | ||
? new Error( | ||
`Failed to deactivate user [${args.userId}] in [${args.organisationUrl}].`, | ||
{ | ||
cause: error, | ||
} | ||
) | ||
: new Error( | ||
`Failed to deactivate user [${args.userId}] in [${ | ||
args.organisationUrl | ||
}] because of [${JSON.stringify(error)}].` | ||
); | ||
} | ||
} | ||
); |
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,71 @@ | ||
import * as okta from '@okta/okta-sdk-nodejs'; | ||
|
||
/** | ||
* Subset of User information provided by Okta. See okta.User for further information on it's derived type. | ||
* @see https://developer.okta.com/docs/reference/api/users/ | ||
*/ | ||
export type User = { | ||
/** The internal Okta identifier. */ | ||
readonly id: string; | ||
/** | ||
* User login string. | ||
*/ | ||
readonly login: string; | ||
/** | ||
* User email adress. | ||
*/ | ||
readonly email: string; | ||
/** | ||
* Name of the user. | ||
*/ | ||
readonly name: string; | ||
/** | ||
* User status as a string. | ||
*/ | ||
readonly status: string; | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types | ||
export const user = (oktaUser: okta.User) => ({ | ||
id: oktaUser.id, | ||
login: oktaUser.profile.login, | ||
email: oktaUser.profile.email, | ||
name: oktaUser.profile.displayName, | ||
status: String(oktaUser.status), | ||
}); | ||
|
||
/** | ||
* Configuration required to create an Okta client. | ||
*/ | ||
export type OktaConfiguration = { | ||
/** The identifier of the client application in Okta. */ | ||
readonly clientId: string; | ||
/** JSON encoded private key for the application. */ | ||
readonly privateKey: string; | ||
/** URL of the Okta organisation. */ | ||
readonly organisationUrl: string; | ||
}; | ||
|
||
/** | ||
* Creates a client that can read user information from Okta. | ||
* @param oktaConfiguration configuration to use when construction the client. | ||
* @returns the Okta client. | ||
*/ | ||
export const oktaReadOnlyClient = (oktaConfiguration: OktaConfiguration) => | ||
new okta.Client({ | ||
...oktaConfiguration, | ||
authorizationMode: 'PrivateKey', | ||
scopes: ['okta.users.read'], | ||
}); | ||
|
||
/** | ||
* Creates a client that can read and manage user information in Okta. | ||
* @param oktaConfiguration configuration to use when construction the client. | ||
* @returns the Okta client. | ||
*/ | ||
export const oktaManageClient = (oktaConfiguration: OktaConfiguration) => | ||
new okta.Client({ | ||
...oktaConfiguration, | ||
authorizationMode: 'PrivateKey', | ||
scopes: ['okta.users.manage'], | ||
}); |