Skip to content

Instantly share code, notes, and snippets.

/adduser.py Secret

Created February 8, 2017 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/9c1e51dad4d779e779d35003ec23d347 to your computer and use it in GitHub Desktop.
Save anonymous/9c1e51dad4d779e779d35003ec23d347 to your computer and use it in GitHub Desktop.
python 2.x script for adding new user to Cloud9 team
#!/usr/bin/env python
# coding: utf-8
# Imports
import urllib2, os, sys
from cookielib import CookieJar
# Set constants from args
TEAM_NAME = sys.argv[1]
ADD_USER_EMAIL = sys.argv[2] # sample command $ python adduser.py YOURTEAMNAMEHERE newuser@example.com
# Set constants from environment variables
USER_NAME = os.environ['C9_USER_NAME'] # add login credentials to environment variables of team admin
PASSPHRASE = os.environ['C9_PASSPHRASE'] # change to sys.argv[3] etc. if you want to pass them in bash
# Initialize the CookieJar to handle cookies for urllib2
cj = CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
# Data is posted to cloud9 as a text data of a JSON object in the body.
login_data = r'{"username": "' + USER_NAME + '", "password": "' + PASSPHRASE + '"}'
# Be careful if your password contains \ (mine did) as even though use of r'' string will make \ raw,
# C9 parser will not see \ unless you escape with \ so write \\ when setting your environment variable
# (if you set env from bash, you need to escape that too, so you would do
# user@linux:~ $ export C9_PASSPHRASE=my\\\\pass
# if your original password has one \ symbol between my and pass
# Create the request object. data is not None so this is POST request
req = urllib2.Request("https://c9.io/auth/login",
data=login_data,
origin_req_host="c9.io")
# Set some headers... some of these may not be necessary
req.add_header("Connection","keep-alive")
req.add_header("Origin","https://c9.io")
req.add_header("x-requested-with","xmlhttprequest")
req.add_header("User-Agent","Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36")
req.add_header("Content-Type","application/json")
req.add_header("Accept","/")
req.add_header("Referer","https://c9.io/login")
req.add_header("Accept-Language","ja,en-US;q=0.8,en;q=0.6,fr-FR;q=0.4,fr;q=0.2")
# Send the request here. We don't need anything from the response, just the cookies which are handled for us.
result1 = urllib2.urlopen(req)
# Second request, this time we need a token this one is GET request as data=None
req = urllib2.Request("https://c9.io/api/nc/auth?client_id=profile_direct&responseType=direct&login_hint=&immediate=1",
origin_req_host="c9.io")
# Set headers again. Different referrer
req.add_header("Connection","keep-alive")
req.add_header("Accept","application/json")
req.add_header("User-Agent","Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36")
req.add_header("Content-Type","application/json")
req.add_header("Referer","https://c9.io/login")
req.add_header("Accept-Language","ja,en-US;q=0.8,en;q=0.6,fr-FR;q=0.4,fr;q=0.2")
# Send GET request
result2 = urllib2.urlopen(req)
# Read the token from the response.
token = result2.read() # Token
# text JSON data with the users email which you want to invite
email_data = r'{"email": "' + ADD_USER_EMAIL + '"}'
# this is a POST request to invite the email address to your team.
req = urllib2.Request("https://api.c9.io/user/org/" + TEAM_NAME + "/invite?access_token=" + token,
data=email_data,
origin_req_host="api.c9.io")
# Headers again, note the Referer uses the team name.
req.add_header("Connection","keep-alive")
req.add_header("Accept","application/json")
req.add_header("Origin","https://c9.io")
req.add_header("User-Agent","Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36")
req.add_header("Content-Type","application/json")
req.add_header("Referer","https://c9.io/team/" + TEAM_NAME + "/manage")
req.add_header("Accept-Language","ja,en-US;q=0.8,en;q=0.6,fr-FR;q=0.4,fr;q=0.2")
# Send the POST request
result3 = urllib2.urlopen(req)
# If you get to this line, the user should be added.
# Otherwise, this python script will throw an error to bash.
# I have not included any error handling logic in this script.
print "Success: Added " + ADD_USER_EMAIL + " to Cloud9 team " + TEAM_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment