1

I want to authenticate GitHub user to my GitHub application and serve to my local server 127.0.0.1:8000, but I am not able to take tokens.

This is how GitHub is showing authentication.

From GitHub documentation, I am not able to understand the process of authentication after generating private key, then how to create JWT and installation tokens ?

Could someone show me what to do next ?

1 Answers1

0

You can follow "Obtaining an Access Token from a GitHub Application Webhook" (Jerrie Pelser), which itself takes from "JWT RSA & HMAC + ASP.NET Core" from Piotr Gankiewicz

Jerrie mentions as first step to convert your PEM file to XML format.
You can use an online tool or write a class

And you need your GitHub application Id:

https://d33wubrfki0l68.cloudfront.net/0833e30d337eefe5d320feab78af158273127740/05e98/static/8eefe1c7f34b1a02a4a8794c16b83c92/3c051/app-identifier.png

You will find in the article the class JwtSecurityTokenHandler used to create the JSON Web Token from the XML key.

In Django:

@api_view(['POST'])
@permission_classes([AllowAny, ])
def authenticate_user(request):
 
    try:
        email = request.data['email']
        password = request.data['password']
 
        user = User.objects.get(email=email, password=password)
        if user:
            try:
                payload = jwt_payload_handler(user)
                token = jwt.encode(payload, settings.SECRET_KEY)
                user_details = {}
                user_details['name'] = "%s %s" % (
                    user.first_name, user.last_name)
                user_details['token'] = token
                user_logged_in.send(sender=user.__class__,
                                    request=request, user=user)
                return Response(user_details, status=status.HTTP_200_OK)
 
            except Exception as e:
                raise e
        else:
            res = {
                'error': 'can not authenticate with the given credentials or the account has been deactivated'}
            return Response(res, status=status.HTTP_403_FORBIDDEN)
    except KeyError:
        res = {'error': 'please provide a email and a password'}
        return Response(res)
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250