~sircmpwn/meta.sr.ht

ref: 72548bd7545f78670878667674cc7645835a17bd meta.sr.ht/metasrht/types/oauthtoken.py -rw-r--r-- 834 bytes
72548bd7Drew DeVault API: Updates per core-go auth changes 1 year, 1 month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import sqlalchemy as sa
from datetime import datetime, timedelta
from srht.database import Base
from srht.oauth import OAuthTokenMixin
import hashlib
import binascii
import os

class OAuthToken(Base, OAuthTokenMixin):
    comment = sa.Column(sa.String(128))

    @property
    def first_party(self):
        return not self.client or self.client.preauthorized

    def __init__(self, user=None, client=None):
        self.user_id = user.id if user else None
        self.client_id = client.id if client else None
        self.expires = datetime.utcnow() + timedelta(days=365)

    def gen_token(self, comment=None):
        token = binascii.hexlify(os.urandom(16)).decode()
        self.token_partial = token[:8]
        self.token_hash = hashlib.sha512(token.encode()).hexdigest()
        self.comment = comment
        return token