|
1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
|
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
|
3
|
|
|
# this work for additional information regarding copyright ownership. |
|
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
|
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
|
6
|
|
|
# the License. You may obtain a copy of the License at |
|
7
|
|
|
# |
|
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
9
|
|
|
# |
|
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
|
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13
|
|
|
# See the License for the specific language governing permissions and |
|
14
|
|
|
# limitations under the License. |
|
15
|
|
|
|
|
16
|
|
|
from st2common.exceptions.auth import (TokenNotFoundError, ApiKeyNotFoundError, |
|
17
|
|
|
UserNotFoundError, AmbiguousUserError, |
|
18
|
|
|
NoNicknameOriginProvidedError) |
|
19
|
|
|
from st2common.models.db import MongoDBAccess |
|
20
|
|
|
from st2common.models.db.auth import UserDB, TokenDB, ApiKeyDB |
|
21
|
|
|
from st2common.persistence.base import Access |
|
22
|
|
|
from st2common.util import hash as hash_utils |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
class User(Access): |
|
26
|
|
|
impl = MongoDBAccess(UserDB) |
|
27
|
|
|
|
|
28
|
|
|
@classmethod |
|
29
|
|
|
def get(cls, username): |
|
|
|
|
|
|
30
|
|
|
return cls.get_by_name(username) |
|
31
|
|
|
|
|
32
|
|
|
@classmethod |
|
33
|
|
|
def get_by_nickname(cls, nickname, origin): |
|
34
|
|
|
if not origin: |
|
35
|
|
|
raise NoNicknameOriginProvidedError() |
|
36
|
|
|
|
|
37
|
|
|
result = cls.query(**{('nickname__%s' % origin): nickname}) |
|
38
|
|
|
|
|
39
|
|
|
if not result.first(): |
|
40
|
|
|
raise UserNotFoundError() |
|
41
|
|
|
if result.count() > 1: |
|
42
|
|
|
raise AmbiguousUserError() |
|
43
|
|
|
|
|
44
|
|
|
return result.first() |
|
45
|
|
|
|
|
46
|
|
|
@classmethod |
|
47
|
|
|
def _get_impl(cls): |
|
48
|
|
|
return cls.impl |
|
49
|
|
|
|
|
50
|
|
|
@classmethod |
|
51
|
|
|
def _get_by_object(cls, object): |
|
|
|
|
|
|
52
|
|
|
# For User name is unique. |
|
53
|
|
|
name = getattr(object, 'name', '') |
|
54
|
|
|
return cls.get_by_name(name) |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
class Token(Access): |
|
58
|
|
|
impl = MongoDBAccess(TokenDB) |
|
59
|
|
|
|
|
60
|
|
|
@classmethod |
|
61
|
|
|
def _get_impl(cls): |
|
62
|
|
|
return cls.impl |
|
63
|
|
|
|
|
64
|
|
|
@classmethod |
|
65
|
|
|
def add_or_update(cls, model_object, publish=True): |
|
|
|
|
|
|
66
|
|
|
if not getattr(model_object, 'user', None): |
|
67
|
|
|
raise ValueError('User is not provided in the token.') |
|
68
|
|
|
if not getattr(model_object, 'token', None): |
|
69
|
|
|
raise ValueError('Token value is not set.') |
|
70
|
|
|
if not getattr(model_object, 'expiry', None): |
|
71
|
|
|
raise ValueError('Token expiry is not provided in the token.') |
|
72
|
|
|
return super(Token, cls).add_or_update(model_object, publish=publish) |
|
73
|
|
|
|
|
74
|
|
|
@classmethod |
|
75
|
|
|
def get(cls, value): |
|
|
|
|
|
|
76
|
|
|
result = cls.query(token=value).first() |
|
77
|
|
|
|
|
78
|
|
|
if not result: |
|
79
|
|
|
raise TokenNotFoundError() |
|
80
|
|
|
|
|
81
|
|
|
return result |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
class ApiKey(Access): |
|
85
|
|
|
impl = MongoDBAccess(ApiKeyDB) |
|
86
|
|
|
|
|
87
|
|
|
@classmethod |
|
88
|
|
|
def _get_impl(cls): |
|
89
|
|
|
return cls.impl |
|
90
|
|
|
|
|
91
|
|
|
@classmethod |
|
92
|
|
|
def get(cls, value): |
|
|
|
|
|
|
93
|
|
|
# DB does not contain key but the key_hash. |
|
94
|
|
|
value_hash = hash_utils.hash(value) |
|
95
|
|
|
result = cls.query(key_hash=value_hash).first() |
|
96
|
|
|
|
|
97
|
|
|
if not result: |
|
98
|
|
|
raise ApiKeyNotFoundError('ApiKey with key_hash=%s not found.' % value_hash) |
|
99
|
|
|
|
|
100
|
|
|
return result |
|
101
|
|
|
|
|
102
|
|
|
@classmethod |
|
103
|
|
|
def get_by_key_or_id(cls, value): |
|
104
|
|
|
try: |
|
105
|
|
|
return cls.get(value) |
|
106
|
|
|
except ApiKeyNotFoundError: |
|
107
|
|
|
pass |
|
108
|
|
|
try: |
|
109
|
|
|
return cls.get_by_id(value) |
|
110
|
|
|
except: |
|
111
|
|
|
raise ApiKeyNotFoundError('ApiKey with key or id=%s not found.' % value) |
|
112
|
|
|
|