ruslan-korneev / glitch

GitLab Interface in Telegram Chat
0 stars 0 forks source link

Think about authorization in gitlab via telegram #3

Closed ruslan-korneev closed 1 year ago

ruslan-korneev commented 1 year ago

OAuth Authorization for users

Source

1. /oauth/authorize

user should go to this link https://${GITLAB_HOST}/oauth/authorize?client_id=${GITLAB_APP_ID}&response_type=code&redirect_uri=${BACKEND_HOST}/${OAUTH_ENDPOINT}

2. /oauth/token

backend sends request to get access token, to next autorizations. example in python

import requests
from requests import auth

# this u can get in your gitlab > profile > preferences > application
GITLAB_APP_ID = os.environ["GITLAB_APP_ID"]
GITLAB_APP_SECRET = os.environ["GITLAB_APP_SECRET"]

def get_token(code: str):
    client_auth = auth.HTTPBasicAuth(GITLAB_APP_ID, GITLAB_APP_SECRET)
    post_data = {
        "grant_type": "authorization_code",
        "code": code,
        "redirect_uri": "https://${BACKEND_HOST}/${OAUTH_ENDPOINT}"
    }
    response = requests.post(
        "https://gitlab.com/oauth/token",
        auth=client_auth,
        data=post_data
    )
    data = response.json()
    return data["access_token"]

Access Token Usage

from gitlab import Gitlab

oauth_access_token = "imagine u got this from previous steps"
gitlab = Gitlab(oauth_token=oauth_access_token)
gitlab.user  # this will return current user, the user whos access token we actually got
ruslan-korneev commented 1 year ago

think about refreshing access token