-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc231e5
commit 3e880d7
Showing
13 changed files
with
468 additions
and
132 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"description": "authorize an org using a device code", | ||
"examples": [ | ||
"sfdx auth:device:login -d -a TestOrg1", | ||
"sfdx auth:device:login -i <OAuth client id>", | ||
"sfdx auth:device:login -r https://test.salesforce.com" | ||
], | ||
"actionRequired": "Action Required!", | ||
"enterCode": "Enter %s user code in the verification URL %s", | ||
"success": "Login successful for %s. You can now close the browser." | ||
} |
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
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,84 @@ | ||
/* | ||
* Copyright (c) 2020, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
import * as os from 'os'; | ||
|
||
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command'; | ||
import { AuthFields, DeviceOauthService, Messages, OAuth2Options } from '@salesforce/core'; | ||
import { Prompts } from '../../../prompts'; | ||
import { Common } from '../../../common'; | ||
|
||
Messages.importMessagesDirectory(__dirname); | ||
const messages = Messages.loadMessages('@salesforce/plugin-auth', 'device.login'); | ||
const commonMessages = Messages.loadMessages('@salesforce/plugin-auth', 'messages'); | ||
|
||
export default class Login extends SfdxCommand { | ||
public static readonly description = messages.getMessage('description'); | ||
public static readonly examples = messages.getMessage('examples').split(os.EOL); | ||
public static readonly flagsConfig: FlagsConfig = { | ||
clientid: flags.string({ | ||
char: 'i', | ||
description: commonMessages.getMessage('clientId'), | ||
}), | ||
instanceurl: flags.url({ | ||
char: 'r', | ||
description: commonMessages.getMessage('instanceUrl'), | ||
}), | ||
setdefaultdevhubusername: flags.boolean({ | ||
char: 'd', | ||
description: commonMessages.getMessage('setDefaultDevHub'), | ||
}), | ||
setdefaultusername: flags.boolean({ | ||
char: 's', | ||
description: commonMessages.getMessage('setDefaultUsername'), | ||
}), | ||
setalias: flags.string({ | ||
char: 'a', | ||
description: commonMessages.getMessage('setAlias'), | ||
}), | ||
disablemasking: flags.boolean({ | ||
description: commonMessages.getMessage('disableMasking'), | ||
hidden: true, | ||
}), | ||
}; | ||
|
||
public async run(): Promise<AuthFields> { | ||
if (await Prompts.shouldExitCommand(this.ux, this.flags.noprompt)) return {}; | ||
|
||
const oauthConfig: OAuth2Options = { | ||
loginUrl: this.flags.instanceurl, | ||
clientId: this.flags.clientid, | ||
}; | ||
|
||
if (this.flags.clientid) { | ||
oauthConfig.clientSecret = await Prompts.askForClientSecret(this.ux, this.flags.disablemasking); | ||
} | ||
|
||
const deviceOauthService = await DeviceOauthService.create(oauthConfig); | ||
const loginData = await deviceOauthService.requestDeviceLogin(); | ||
|
||
if (this.flags.json) { | ||
this.ux.logJson(loginData); | ||
} else { | ||
this.ux.styledHeader(messages.getMessage('actionRequired')); | ||
this.ux.log(messages.getMessage('enterCode'), loginData.user_code, loginData.verification_uri); | ||
this.ux.log(); | ||
} | ||
|
||
const approval = await deviceOauthService.awaitDeviceApproval(loginData); | ||
if (approval) { | ||
const authInfo = await deviceOauthService.authorizeAndSave(approval); | ||
await Common.handleSideEffects(authInfo, this.flags); | ||
const fields = authInfo.getFields(); | ||
const successMsg = messages.getMessage('success', [fields.username]); | ||
this.ux.log(successMsg); | ||
return fields; | ||
} else { | ||
return {}; | ||
} | ||
} | ||
} |
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
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,27 @@ | ||
/* | ||
* Copyright (c) 2020, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
import { AuthInfo } from '@salesforce/core'; | ||
|
||
interface Flags { | ||
setalias?: string; | ||
setdefaultdevhubusername?: boolean; | ||
setdefaultusername?: boolean; | ||
} | ||
|
||
export class Common { | ||
public static async handleSideEffects(authInfo: AuthInfo, flags: Flags): Promise<void> { | ||
if (flags.setalias) await authInfo.setAlias(flags.setalias); | ||
|
||
if (flags.setdefaultdevhubusername || flags.setdefaultusername) { | ||
await authInfo.setAsDefault({ | ||
defaultUsername: flags.setdefaultusername, | ||
defaultDevhubUsername: flags.setdefaultdevhubusername, | ||
}); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.