Skip to content

Instantly share code, notes, and snippets.

@scr34m
Forked from kartoch/hubic_credentials.py
Created July 8, 2014 12:23
Show Gist options
  • Save scr34m/f11978e0d9550a59b139 to your computer and use it in GitHub Desktop.
Save scr34m/f11978e0d9550a59b139 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
import base64
import json
import logging
import requests
import sys
from swiftclient import client
class AuthenticationHubic:
def __init__(self, user, passwd):
self.logger = logging.getLogger(__name__)
self.SessionHandler = 'https://ws.ovh.com/sessionHandler/r4/'
self.hubicws = 'https://ws.ovh.com/hubic/r5/'
r = requests.get(self.SessionHandler + 'rest.dispatcher/' + 'getAnonymousSession')
sessionId = r.json()['answer']['session']['id']
self.logger.info("sessionId:" + sessionId)
params = { 'sessionId': sessionId,
'email': user}
payload = {'params': json.dumps(params)}
r = requests.get(self.hubicws + 'rest.dispatcher/' + 'getHubics',
params=payload)
hubics = r.json()
self.hubicsId = hubics['answer'][0]['id']
self.logger.info("hubicsId:" + self.hubicsId)
params = { 'login': hubics['answer'][0]['nic'],
'password': passwd,
'context': 'hubic'}
payload = {'params': json.dumps(params)}
r = requests.get(self.SessionHandler + 'rest.dispatcher/' + 'login',
params=payload)
self.sessionId = r.json()['answer']['session']['id']
self.logger.info("authenticated hubicsId:" + self.sessionId)
def get_credentials(self):
params = { 'sessionId': self.sessionId,
'hubicId': self.hubicsId}
payload = {'params': json.dumps(params)}
r = requests.get(self.hubicws + 'rest.dispatcher/' + 'getHubic',
params=payload)
Storage_Url = base64.b64decode(r.json()['answer']['credentials']['username'])
self.logger.info("storage url:" + str(Storage_Url))
Auth_Token = r.json()['answer']['credentials']['secret']
self.logger.info("auth token:" + Auth_Token)
return Storage_Url, Auth_Token
def logout(self):
params = { 'sessionId': self.sessionId}
payload = {'params': json.dumps(params)}
r = requests.get(self.SessionHandler + 'rest.dispatcher/' + 'logout',
params=payload)
if __name__ == '__main__':
logging.basicConfig(level = logging.ERROR)
if len(sys.argv) != 3:
logging.error("Usage: sys.argv[0] login password")
sys.exit(1)
login = sys.argv[1]
passwd = sys.argv[2]
auth = AuthenticationHubic(login, passwd)
storage_url, auth_token = auth.get_credentials()
print("export SWIFT_PREAUTHURL=\"" + str(storage_url) +"\";" )
print("export SWIFT_PREAUTHTOKEN=\"" + auth_token + "\"")
auth.logout()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment