Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
common-web
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
protokollsystem
common-web
Commits
40b4b48f
Commit
40b4b48f
authored
8 years ago
by
Robin Sonnabend
Browse files
Options
Downloads
Patches
Plain Diff
Implemented logging in and out
parents
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
auth.py
+67
-0
67 additions, 0 deletions
auth.py
with
67 additions
and
0 deletions
auth.py
0 → 100644
+
67
−
0
View file @
40b4b48f
import
ldap
import
hmac
,
hashlib
class
User
:
def
__init__
(
self
,
username
,
groups
):
self
.
username
=
username
self
.
groups
=
groups
def
summarize
(
self
):
return
"
{}:{}
"
.
format
(
self
.
username
,
"
,
"
.
join
(
self
.
groups
))
@staticmethod
def
from_summary
(
summary
):
name
,
groupstring
=
summary
.
split
(
"
:
"
,
1
)
groups
=
groupstring
.
split
(
"
,
"
)
return
User
(
name
,
groups
)
@staticmethod
def
from_hashstring
(
secure_string
):
summary
,
hash
=
secure_string
.
split
(
"
=
"
,
1
)
return
User
.
from_summary
(
summary
)
class
LdapManager
:
def
__init__
(
self
,
url
,
base
):
self
.
connection
=
ldap
.
initialize
(
url
)
self
.
base
=
base
def
login
(
self
,
username
,
password
):
if
not
self
.
authenticate
(
username
,
password
):
return
None
groups
=
list
(
map
(
lambda
g
:
g
.
decode
(
"
utf-8
"
),
self
.
groups
(
username
)))
print
(
groups
)
return
User
(
username
,
groups
)
def
authenticate
(
self
,
username
,
password
):
try
:
self
.
connection
.
simple_bind_s
(
"
uid={},ou=users,{}
"
.
format
(
username
,
self
.
base
),
password
)
return
True
except
ldap
.
INVALID_CREDENTIALS
:
return
False
return
False
def
groups
(
self
,
username
):
result
=
[]
for
_
,
result_dict
in
self
.
connection
.
search_s
(
self
.
base
,
ldap
.
SCOPE_SUBTREE
,
"
(memberUid={})
"
.
format
(
username
),
[
"
cn
"
]):
result
.
append
(
result_dict
[
"
cn
"
][
0
])
return
result
class
SecurityManager
:
def
__init__
(
self
,
key
):
self
.
maccer
=
hmac
.
new
(
key
.
encode
(
"
utf-8
"
),
digestmod
=
hashlib
.
sha512
)
def
hash_user
(
self
,
user
):
maccer
=
self
.
maccer
.
copy
()
summary
=
user
.
summarize
()
maccer
.
update
(
summary
.
encode
(
"
utf-8
"
))
return
"
{}={}
"
.
format
(
summary
,
maccer
.
hexdigest
())
def
check_user
(
self
,
string
):
parts
=
string
.
split
(
"
=
"
,
1
)
if
len
(
parts
)
!=
2
:
# wrong format, expecting summary:hash
return
False
summary
,
hash
=
map
(
lambda
s
:
s
.
encode
(
"
utf-8
"
),
parts
)
maccer
=
self
.
maccer
.
copy
()
maccer
.
update
(
summary
)
return
hmac
.
compare_digest
(
maccer
.
hexdigest
().
encode
(
"
utf-8
"
),
hash
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment