lab132 / buildbot-gitea

Buildbot plugin for integration with gitea.
MIT License
62 stars 21 forks source link

Gitea oauth2 class #4

Closed tcrowe closed 4 years ago

tcrowe commented 4 years ago

Hi 👋 I'm tapping into your module here and I added this class:

from buildbot.plugins import util
from buildbot.www.oauth2 import OAuth2Auth
import os

class GiteaAuth(OAuth2Auth):
  name = 'Gitea'
  faIcon = 'mug-tea'
  resourceEndpoint = os.environ['BUILDBOT_GITEA_RESOURCE_ENDPOINT']
  authUri = os.environ['BUILDBOT_GITEA_AUTH_URI']
  tokenUri = os.environ['BUILDBOT_GITEA_TOKEN_URI']
  def getUserInfoFromOAuthClient(self, c):
    return self.get(c, '/api/v1/user')

The JSON response seems to be in the exact format it's looking for. https://github.com/buildbot/buildbot/blob/master/master/buildbot/www/oauth2.py

Do you want this in? If so can you guide me to the right path and filename? I am inexperienced in Python modules.

Thank you

tcrowe commented 4 years ago

Oh, and the environment I have is like this.

export BUILDBOT_GITEA_RESOURCE_ENDPOINT=https://your-gitea--host
export BUILDBOT_GITEA_AUTH_URI=https://your-gitea--host/login/oauth/authorize
export BUILDBOT_GITEA_TOKEN_URI=https://your-gitea--host/login/oauth/access_token
buildbot start .

It works :D

tcrowe commented 4 years ago

Pardon many comments. There is also the master.cfg:

from auth import GiteaAuth
c['www']['auth'] = GiteaAuth('oauth2-client-id', 'oauth2-client-secret')
pampersrocker commented 4 years ago

Hey! This look interesting. But instead of using environment variables you would set those values using a constructor so you can pass it in to the class like this:

from auth import GiteaAuth
c['www']['auth'] = GiteaAuth(
    endpoint="https://your-gitea--host",
    client_id 'oauth2-client-id',
    client_secret='oauth2-client-secret')

AUTH_URI and TOKEN_URI should be unnecessary unless they change per gitea installation, otherwise you would just construct them from the endpoint e.g. "{}/login/auth/authorize".format(endpoint)

From the structural side, just create a auth.py inside the buildbot_gitea directory, next to reporter.py. You would also need to add it as an entry point into setup.py so the buildbot plugin infrastructure picks it up and one can use it using from buildbot.plugins.auth import GiteaAuth but I can help with that. Lastly unit tests should be created to test this. The OAuth implementations from GitHub or GitLab into buildbot might be a good reference for this.

tcrowe commented 4 years ago

Thank you for the guidance. I will give it a try.

pampersrocker commented 4 years ago

Continued in #5