Skip to content

Commit 1524d18

Browse files
committed
Convert ldap-web-api to Git repo
0 parents  commit 1524d18

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

main.py

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env python
2+
3+
from flask import Flask, render_template
4+
import ldap
5+
from datetime import timedelta
6+
from flask import make_response, request, current_app
7+
from functools import update_wrapper
8+
9+
10+
def crossdomain(origin=None, methods=None, headers=None,
11+
max_age=21600, attach_to_all=True,
12+
automatic_options=True):
13+
if methods is not None:
14+
methods = ', '.join(sorted(x.upper() for x in methods))
15+
if headers is not None and not isinstance(headers, basestring):
16+
headers = ', '.join(x.upper() for x in headers)
17+
if not isinstance(origin, basestring):
18+
origin = ', '.join(origin)
19+
if isinstance(max_age, timedelta):
20+
max_age = max_age.total_seconds()
21+
22+
def get_methods():
23+
if methods is not None:
24+
return methods
25+
26+
options_resp = current_app.make_default_options_response()
27+
return options_resp.headers['allow']
28+
29+
def decorator(f):
30+
def wrapped_function(*args, **kwargs):
31+
if automatic_options and request.method == 'OPTIONS':
32+
resp = current_app.make_default_options_response()
33+
else:
34+
resp = make_response(f(*args, **kwargs))
35+
if not attach_to_all and request.method != 'OPTIONS':
36+
return resp
37+
38+
h = resp.headers
39+
40+
h['Access-Control-Allow-Origin'] = origin
41+
h['Access-Control-Allow-Methods'] = get_methods()
42+
h['Access-Control-Max-Age'] = str(max_age)
43+
if headers is not None:
44+
h['Access-Control-Allow-Headers'] = headers
45+
return resp
46+
47+
f.provide_automatic_options = False
48+
return update_wrapper(wrapped_function, f)
49+
return decorator
50+
51+
52+
app = Flask(__name__)
53+
app.debug = False
54+
55+
56+
@app.route("/")
57+
def index():
58+
return ""
59+
60+
61+
def version():
62+
return "CV Restful Ldap - Version 0.0.1"
63+
64+
65+
@app.route("/<uid>/<field>")
66+
@crossdomain(origin='*')
67+
def ldapQuery(uid, field):
68+
return str(getLdap(uid, field))
69+
70+
71+
@app.route("/<uid>/<field>/<element>")
72+
@crossdomain(origin='*')
73+
def ldapQueryElement(uid, field, element):
74+
return str(getLdap(uid, field)[int(element)])
75+
76+
77+
def getLdap(uid, field):
78+
try:
79+
l = ldap.initialize("ldap://ldap.collegiumv.org")
80+
l.simple_bind_s()
81+
result = l.search_s("ou=people,dc=collegiumv,dc=org", ldap.SCOPE_SUBTREE,
82+
"(uid={0})".format(uid), attrlist=[str(field)])
83+
l.unbind()
84+
if len(result) == 0:
85+
return "No user found"
86+
return result[0][1].itervalues().next()
87+
except ldap.LDAPError, e:
88+
return "An error occurred: %s" % e
89+
except Exception as e:
90+
print e
91+
92+
93+
if __name__ == "__main__":
94+
host = "localhost"
95+
port = 7291
96+
app.run(host, port)

uwsgi.ini

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[uwsgi]
2+
socket = /tmp/ldap-web-api.sock
3+
chmod-socket = 660
4+
chown-socket = nginx:cv_account
5+
chdir = /opt/ldap-web-api/
6+
uid = cv_account
7+
gid = cv_account
8+
wsgi-file = main.py
9+
callable = app
10+
master = true
11+
processes = 4
12+
threads = 2
13+
plugin = python

0 commit comments

Comments
 (0)