Harbor: In python, How to use RESTful api to login?

Created on 27 Nov 2019  路  6Comments  路  Source: goharbor/harbor

At version 1.9.3, I want to use api to login, like"/api/login",which is POST method,and the parameters are principal and password.BUT,this api does not work well,and returns 405 Please tell me how to solve it, Thank you very much

Most helpful comment

Hi @hulu888 , there's no /api/login as harbor's RESTful API, as esantoro's replied, you can attach an "Authorization" header in your pathon code for any RESTful APIs, like /api/registries or others.

Here is example for you:
url = "https://"+args.endpoint+"/api/"

def create_project(self, project_name):
body=dict(body={"project_name": ""+project_name+"", "metadata": {"public": "true"}})
request(url+"projects", 'post', **body)

def request(url, method, user = None, userp = None, *kwargs):
if user is None:
user = "admin"
if userp is None:
userp = "Harbor12345"
kwargs.setdefault('headers', kwargs.get('headers', {}))
kwargs['headers']['Accept'] = 'application/json'
if 'body' in kwargs:
kwargs['headers']['Content-Type'] = 'application/json'
kwargs['data'] = json.dumps(kwargs['body'])
del kwargs['body']
resp = requests.request(method, url, verify=False, auth=(user, userp), *
kwargs)
if resp.status_code >= 400:
raise Exception("[Exception Message] - {}".format(resp.text))
return resp

All 6 comments

In version 1.9.3, please do not carry cookies in your POST request, then it will get 201.

In 1.9.0, I'm attaching an "Authorization" header to all requests (HTTP basic auth).
Given that all communications happen via ssl through a vpn, it's reasonably secure.

token = base64.b64encode(bytes("%s:%s" % (username,password), 'ascii'))
auth_header = "Basic %s" % token.decode('ascii')
headers = {'Authorization': auth_header}

resp = requests.get(f"{endpoint}/api/users/current",headers=headers)
# checks whether login is successful / credentials are correct
resp.status_code == 200 

In version 1.9.3, please do not carry cookies in your POST request, then it will get 201.

Could you please tell me a little bit more, I use the requests module to send requests and not carry cookies

Hi @hulu888 , there's no /api/login as harbor's RESTful API, as esantoro's replied, you can attach an "Authorization" header in your pathon code for any RESTful APIs, like /api/registries or others.

Here is example for you:
url = "https://"+args.endpoint+"/api/"

def create_project(self, project_name):
body=dict(body={"project_name": ""+project_name+"", "metadata": {"public": "true"}})
request(url+"projects", 'post', **body)

def request(url, method, user = None, userp = None, *kwargs):
if user is None:
user = "admin"
if userp is None:
userp = "Harbor12345"
kwargs.setdefault('headers', kwargs.get('headers', {}))
kwargs['headers']['Accept'] = 'application/json'
if 'body' in kwargs:
kwargs['headers']['Content-Type'] = 'application/json'
kwargs['data'] = json.dumps(kwargs['body'])
del kwargs['body']
resp = requests.request(method, url, verify=False, auth=(user, userp), *
kwargs)
if resp.status_code >= 400:
raise Exception("[Exception Message] - {}".format(resp.text))
return resp

Hi @hulu888 , there's no /api/login as harbor's RESTful API, as esantoro's replied, you can attach an "Authorization" header in your pathon code for any RESTful APIs, like /api/registries or others.

Here is example for you:
url = "https://"+args.endpoint+"/api/"

def create_project(self, project_name):
body=dict(body={"project_name": ""+project_name+"", "metadata": {"public": "true"}})
request(url+"projects", 'post', **body)

def request(url, method, user = None, userp = None, *kwargs):
if user is None:
user = "admin"
if userp is None:
userp = "Harbor12345"
kwargs.setdefault('headers', kwargs.get('headers', {}))
kwargs['headers']['Accept'] = 'application/json'
if 'body' in kwargs:
kwargs['headers']['Content-Type'] = 'application/json'
kwargs['data'] = json.dumps(kwargs['body'])
del kwargs['body']
resp = requests.request(method, url, verify=False, auth=(user, userp), *
kwargs)
if resp.status_code >= 400:
raise Exception("[Exception Message] - {}".format(resp.text))
return resp

I got it, thank you very much.

In 1.9.0, I'm attaching an "Authorization" header to all requests (HTTP basic auth).
Given that all communications happen via ssl through a vpn, it's reasonably secure.

token = base64.b64encode(bytes("%s:%s" % (username,password), 'ascii'))
auth_header = "Basic %s" % token.decode('ascii')
headers = {'Authorization': auth_header}

resp = requests.get(f"{endpoint}/api/users/current",headers=headers)
# checks whether login is successful / credentials are correct
resp.status_code == 200 

thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

272909106 picture 272909106  路  4Comments

levchik picture levchik  路  4Comments

reasonerjt picture reasonerjt  路  3Comments

xiaosadexiaohai picture xiaosadexiaohai  路  3Comments

steveal picture steveal  路  3Comments