TritonDataCenter / node-docker-registry-client

node.js client for the docker registry
Mozilla Public License 2.0
64 stars 33 forks source link

Allow V2 bearer token authentication. #6

Closed walac closed 8 years ago

walac commented 8 years ago

V2 ping function doesn't provide a way for bearer token authentication. With this commit, we pass the this._authInfo to ping function, and it is valid, we use it.

Sample code:

var docker = require('docker-registry-client');

var client = docker.createClientV2({name: 'taskcluster'});

client.ping(function(err, body, res) {
  if (err.statusCode == 401) {
    docker.loginV2({
      pingErr: err,
      pingRes: res,
      indexName: client.repo.index.name
    }, function(err, result) {
      client._authInfo = result.authInfo;

      client.ping(function(err, body, res) {
        if (!err) {
          console.log('Success!');
        } else {
          console.log('Fail...');
        }
      });
    });
  }
});
trentm commented 8 years ago

@walac Thanks much! I took your changes, and also exposed .login(...) on the client so one doesn't have to hack in client._authInfo. Example code:

var drc = require('docker-registry-client');
var bunyan = require('bunyan');

var log = bunyan.createLogger({name: 'pingplay', level: 'trace'});
var client = drc.createClientV2({
    name: 'docker.io',
    log: log,
    username: 'MY-USERNAME',
    password: 'MY-PASSWORD'
});

client.ping(function(err, body, res) {
    if (!err) {
        console.log('Success on first try');
    } else if (err.statusCode === 401) {
        client.login({
            pingErr: err,
            pingRes: res
        }, function(err, result) {
            client.ping(function(err, body, res) {
                if (!err) {
                    console.log('Success');
                } else {
                    console.log('Fail:', err);
                }
            });
        });
    } else {
        console.log('Huh?', err);
    }
});

This is published in docker-registry-client@3.1.0.

walac commented 8 years ago

Great, thank you very much.