postgresql.py 5.33 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Copyright (c) 2017, Quang-Nhat HOANG-XUAN
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" Store adapters to read/write data to from/to PostgresSQL. """

import zoe_lib.state

from zoe_lib.config import get_conf
hxquangnhat's avatar
hxquangnhat committed
21
22
23
from oauth2.store import AccessTokenStore, ClientStore
from oauth2.datatype import AccessToken, Client
from oauth2.error import AccessTokenNotFound, ClientNotFoundError
24

hxquangnhat's avatar
hxquangnhat committed
25
26
class AccessTokenStorePg(AccessTokenStore):
    """ AccessTokenStore for postgresql  """
27
28

    def fetch_by_refresh_token(self, refresh_token):
hxquangnhat's avatar
hxquangnhat committed
29
        """ get accesstoken from refreshtoken """
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
        sql = zoe_lib.state.SQLManager(get_conf())
        data = sql.fetch_by_refresh_token(refresh_token)

        if data is None:
            raise AccessTokenNotFound

        return AccessToken(client_id=data["client_id"],
                           grant_type=data["grant_type"],
                           token=data["token"],
                           data=data["data"],
                           expires_at=data["expires_at"].timestamp(),
                           refresh_token=data["refresh_token"],
                           refresh_expires_at=data["refresh_token_expires_at"].timestamp(),
                           scopes=data["scopes"])

    def delete_refresh_token(self, refresh_token):
        """
        Deletes (invalidates) an old refresh token after use
        :param refresh_token: The refresh token.
        """
        sql = zoe_lib.state.SQLManager(get_conf())
        res = sql.delete_refresh_token(refresh_token)
        return res

54
    def get_client_id_by_refresh_token(self, refresh_token):
hxquangnhat's avatar
hxquangnhat committed
55
        """ get clientID from refreshtoken """
56
57
58
59
60
        sql = zoe_lib.state.SQLManager(get_conf())
        data = sql.get_client_id_by_refresh_token(refresh_token)

        return data

61
    def get_client_id_by_access_token(self, access_token):
hxquangnhat's avatar
hxquangnhat committed
62
        """ get clientID from accesstoken """
63
64
65
66
67
68
        sql = zoe_lib.state.SQLManager(get_conf())
        data = sql.get_client_id_by_access_token(access_token)

        return data

    def fetch_existing_token_of_user(self, client_id, grant_type, user_id):
hxquangnhat's avatar
hxquangnhat committed
69
        """ get accesstoken from userid """
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
        sql = zoe_lib.state.SQLManager(get_conf())
        data = sql.fetch_existing_token_of_user(client_id, grant_type, user_id)

        if data is None:
            raise AccessTokenNotFound

        return AccessToken(client_id=data["client_id"],
                           grant_type=data["grant_type"],
                           token=data["token"],
                           data=data["data"],
                           expires_at=data["expires_at"].timestamp(),
                           refresh_token=data["refresh_token"],
                           refresh_expires_at=data["refresh_token_expires_at"].timestamp(),
                           scopes=data["scopes"],
                           user_id=data["user_id"])

    def save_token(self, access_token):
hxquangnhat's avatar
hxquangnhat committed
87
        """ save accesstoken """
88
89
        sql = zoe_lib.state.SQLManager(get_conf())
        sql.save_token(access_token.client_id,
hxquangnhat's avatar
hxquangnhat committed
90
91
92
93
94
95
96
97
                       access_token.grant_type,
                       access_token.token,
                       access_token.data,
                       access_token.expires_at,
                       access_token.refresh_token,
                       access_token.refresh_expires_at,
                       access_token.scopes,
                       access_token.user_id)
98
99
100
101

        return True


hxquangnhat's avatar
hxquangnhat committed
102
103
class ClientStorePg(ClientStore):
    """ ClientStore for postgres """
104
105

    def save_client(self, identifier, secret, role, redirect_uris, authorized_grants, authorized_response_types):
hxquangnhat's avatar
hxquangnhat committed
106
        """ save client to db """
107
108
109
110
111
112
113
114
115
116
        sql = zoe_lib.state.SQLManager(get_conf())
        sql.save_client(identifier,
                        secret,
                        role,
                        redirect_uris,
                        authorized_grants,
                        authorized_response_types)
        return True

    def fetch_by_client_id(self, client_id):
hxquangnhat's avatar
hxquangnhat committed
117
        """ get client by clientid """
118
119
120
        sql = zoe_lib.state.SQLManager(get_conf())
        client_data = sql.fetch_by_client_id(client_id)

hxquangnhat's avatar
hxquangnhat committed
121
        client_data_grants = client_data["authorized_grants"].split(':')
122
123
124
125
126
127
128
129
130
131
132

        if client_data is None:
            raise ClientNotFoundError

        return Client(identifier=client_data["identifier"],
                      secret=client_data["secret"],
                      redirect_uris=client_data["redirect_uris"],
                      authorized_grants=client_data_grants,
                      authorized_response_types=client_data["authorized_response_types"])

    def get_role_by_client_id(self, client_id):
hxquangnhat's avatar
hxquangnhat committed
133
        """ get client role by clientid """
134
135
136
137
138
139
140
        sql = zoe_lib.state.SQLManager(get_conf())
        client_data = sql.fetch_by_client_id(client_id)

        if client_data is None:
            raise ClientNotFoundError

        return client_data["role"]