Skip to content

Commit

Permalink
Resolve some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
RulaAbuHasna committed Aug 12, 2024
1 parent 3ca3e5f commit 9601baf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
21 changes: 4 additions & 17 deletions service_auth/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def validate_gh_call_params(code, state):
raise ValidationError("Missing code parameter")
if not state:
raise ValidationError("Missing state parameter")
if "-" not in state or len(state.split("-")) != 2:
raise ValidationError("Invalid state parameter")


def get_github_user(access_token):
Expand Down Expand Up @@ -151,7 +153,7 @@ def get_endpoint_details(
return endpoint


def notify_user_of_error(user, channel_id=None):
def notify_user(user, channel_id=None, message=""):
team_id = user.team_id
installation = SlackInstallation.objects.filter(team_id=team_id).first()
if not installation:
Expand All @@ -162,20 +164,5 @@ def notify_user_of_error(user, channel_id=None):
client = WebClient(token=installation.bot_token)
client.chat_postMessage(
channel=channel_id or user.user_id,
text=f"Error creating Codecov access token for {user.username}, are you sure you have a Codecov account?",
)


def notify_user_of_successful_auth(user, channel_id=None):
team_id = user.team_id
installation = SlackInstallation.objects.filter(team_id=team_id).first()
if not installation:
return Response(
{"detail": f"Slack installation not found {team_id}"}, status=404
)

client = WebClient(token=installation.bot_token)
client.chat_postMessage(
channel=channel_id or user.user_id,
text=f"Successfully authenticated with Codecov",
text=message,
)
21 changes: 11 additions & 10 deletions service_auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from rest_framework.views import APIView

from .actions import create_new_codecov_access_token
from .helpers import (get_github_user, notify_user_of_error,
notify_user_of_successful_auth, validate_gh_call_params)
from .helpers import get_github_user, notify_user, validate_gh_call_params
from .models import Service, SlackUser

GITHUB_CLIENT_ID = os.environ.get("GITHUB_CLIENT_ID")
Expand All @@ -28,14 +27,14 @@ def get(self, request, format=None):
# Get the authorization code from the GitHub's callback request
code = request.GET.get("code")
state = request.GET.get("state")
validate_gh_call_params(code, state)

user_id_state = state.split("-")[0]
user_id = jwt.decode(
user_id_state, USER_ID_SECRET, algorithms=["HS256"]
)["user_id"]
channel_id = state.split("-")[1]

validate_gh_call_params(code, state)

# Exchange the authorization code for an access token
headers = {"Accept": "application/json"}
data = {
Expand Down Expand Up @@ -91,17 +90,19 @@ def get(self, request, format=None):
try:
create_new_codecov_access_token(user)
except Exception as e:
notify_user_of_error(user, channel_id)
message = "Error creating Codecov access token, are you sure you have a Codecov account?"
error = notify_user(user, channel_id, message=message)
if error:
return error

return Response(
{
"detail": "Error creating Codecov access token, are you sure you have a Codecov account?"
},
status=400,
{"detail": "Error creating Codecov access token"}, status=400
)

# redirect to slack app
team_id = user.team_id
slack_url = f"https://slack.com/app_redirect?app={SLACK_APP_ID}&channel={channel_id}&team={team_id}"

notify_user_of_successful_auth(user, channel_id)
message = "Successfully connected your GitHub account to Codecov! You can now use Codecov commands in this channel."
notify_user(user, channel_id, message=message)
return redirect(slack_url)

0 comments on commit 9601baf

Please sign in to comment.