コード例 #1
0
ファイル: auth.js プロジェクト: dmose/volo
                        res.on('end', function () {
                            var config = voloConfig.getLocal();
                            if (!hasError) {
                                body = JSON.parse(body);

                                config[configKey] = {
                                    user: name,
                                    token: body.token,
                                    scopes: authConfig.scopes
                                };

                                v.prompt('Save OAuth token for later use [y]?')
                                .then(function (save) {
                                    save = save && save.toLowerCase();
                                    if (!save || save === 'y' || save === 'yes') {
                                        var saveLocation = voloConfig.saveLocal();
                                        if (saveLocation) {
                                            log('Token saved in ' + saveLocation);
                                        }
                                    }
                                    return config[configKey];
                                })
                                .then(d.resolve, d.reject);
                            }
                        });
コード例 #2
0
ファイル: auth.js プロジェクト: dmose/volo
        fetch: function(options, callback, errback) {
            var d = qutil.convert(callback, errback),
                v = options.v,
                localConfig = voloConfig.getLocal(),
                name;

            //If already have a token, wrap it up.
            if (localConfig[configKey]) {
                d.resolve(localConfig[configKey]);
                return d.promise;
            }

            function log(msg) {
                if (!options.silent) {
                    console.log(msg);
                }
            }

            log('Log in to GitHub to complete action ' +
                        '(your password is not saved. It is sent over ' +
                        'SSL to GitHub and converted to an OAuth token)');
            v.prompt('GitHub user name:')
                .then(function (promptName) {
                    name = promptName;
                    return v.promptHidden('GitHub password:');
                }).then(function (pw) {
                    log('\nContacting GitHub...');
                    var basicAuth = new Buffer(name + ':' + pw)
                                    .toString('base64'),
                        requestBody = JSON.stringify({
                            scopes: authConfig.scopes,
                            note: authConfig.note,
                            note_url: authConfig.noteUrl
                        }),
                        urlParts = url.parse(authConfig.domain + authConfig.authPath),
                        req;

                    urlParts.method = 'POST';

                    req = https.request(urlParts, function (res) {
                        var body = '',
                            hasError;

                        if (res.statusCode === 401) {
                            v.prompt('Incorrect GitHub user name or password. Retry [y]?')
                            .then(function (answer) {
                                answer = answer && answer.toLowerCase();
                                if (!answer || answer.indexOf('y') === 0) {
                                    d.resolve(auth.fetch(options));
                                } else {
                                    d.resolve();
                                }
                            })
                            .fail(d);
                            return;
                        } else if (res.statusCode !== 201) {
                            log('GitHub responded with error status: ' +
                                     res.statusCode);
                            d.resolve();
                            return;
                        }

                        res.setEncoding('utf8');

                        res.on('data', function (chunk) {
                            body += chunk;
                        });

                        res.on('error', function (err) {
                            hasError = true;
                            log('GitHub responded with error: ' + err.toString());
                            d.resolve();
                        });

                        res.on('end', function () {
                            var config = voloConfig.getLocal();
                            if (!hasError) {
                                body = JSON.parse(body);

                                config[configKey] = {
                                    user: name,
                                    token: body.token,
                                    scopes: authConfig.scopes
                                };

                                v.prompt('Save OAuth token for later use [y]?')
                                .then(function (save) {
                                    save = save && save.toLowerCase();
                                    if (!save || save === 'y' || save === 'yes') {
                                        var saveLocation = voloConfig.saveLocal();
                                        if (saveLocation) {
                                            log('Token saved in ' + saveLocation);
                                        }
                                    }
                                    return config[configKey];
                                })
                                .then(d.resolve, d.reject);
                            }
                        });

                    });

                    req.setHeader('Authorization', 'Basic ' + basicAuth);
                    req.setHeader('Content-Type',
                                  'application/json; charset=UTF-8');
                    req.setHeader('Content-Length', requestBody.length);
                    req.end(requestBody);

                }).fail(d);

            return d.promise;
        }