Skip to content

Commit

Permalink
Fix the issue with "AtlassianSecurityContext" being nil after migrati…
Browse files Browse the repository at this point in the history
…on to oauth (#1002)

* [MI-3733] Fix the issue with "AtlassianSecurityContext" being nil after migration to oauth

* most relevant test cases are implemented

* write more test cases. fix issue with oauth instance with no jwt instance installed

* write tests for Jira Server

* fix lint

* add tests for installing oauth instance twice

* remove duplicate line of code

* clarify comment

* Merge branch 'cloud-oauth-migration-unit-test' of https://github.com/mattermost/mattermost-plugin-jira into cloud-oauth-migration-unit-test

---------

Co-authored-by: raghavaggarwal2308 <[email protected]>
  • Loading branch information
mickmister and raghavaggarwal2308 authored Dec 4, 2023
1 parent 8657e65 commit 8372a0c
Show file tree
Hide file tree
Showing 4 changed files with 630 additions and 4 deletions.
13 changes: 9 additions & 4 deletions server/instance_cloud_oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type PKCEParams struct {
}

var _ Instance = (*cloudOAuthInstance)(nil)
var jiraOAuthAccessibleResourcesURL = "https://api.atlassian.com/oauth/token/accessible-resources"

const (
JiraScopes = "read:jira-user,read:jira-work,write:jira-work"
Expand Down Expand Up @@ -132,9 +133,13 @@ func (ci *cloudOAuthInstance) getClientForConnection(connection *Connection) (*j
ctx := context.Background()

// Checking if this user's connection is for a JWT instance
if ci.JWTInstance != nil && connection.OAuth2Token == nil {
ci.Plugin.API.LogDebug("Returning a JWT token client since the stored JWT instance is not nil and the user's oauth token is nil")
return ci.JWTInstance.getClientForConnection(connection)
if connection.OAuth2Token == nil {
if ci.JWTInstance != nil {
ci.Plugin.API.LogDebug("Returning a JWT token client since the stored JWT instance is not nil and the user's oauth token is nil")
return ci.JWTInstance.getClientForConnection(connection)
}

return nil, nil, errors.New("failed to create client for OAuth instance: no JWT instance found, and connection's OAuth token is missing")
}

tokenSource := oauth2Conf.TokenSource(ctx, connection.OAuth2Token)
Expand Down Expand Up @@ -227,7 +232,7 @@ func (ci *cloudOAuthInstance) GetMattermostKey() string {
func (ci *cloudOAuthInstance) getJiraCloudResourceID(client http.Client) (string, error) {
request, err := http.NewRequest(
http.MethodGet,
"https://api.atlassian.com/oauth/token/accessible-resources",
jiraOAuthAccessibleResourcesURL,
nil,
)
if err != nil {
Expand Down
Loading

0 comments on commit 8372a0c

Please sign in to comment.