Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

19.1 KB
Raw Blame
package oauth_test

import (
	"context"
	"crypto/tls"
	"net/http/httptest"
	"net/url"
	"strings"
	"testing"

	"github.com/getfider/fider/app"
	"github.com/getfider/fider/app/models/cmd"
	"github.com/getfider/fider/app/models/entity"
	"github.com/getfider/fider/app/models/enum"

	"github.com/getfider/fider/app/models/query"

	"github.com/getfider/fider/app/pkg/bus"
	"github.com/getfider/fider/app/pkg/errors"
	"github.com/getfider/fider/app/pkg/jwt"
	"github.com/getfider/fider/app/pkg/web"

	. "github.com/getfider/fider/app/pkg/assert"
	"github.com/getfider/fider/app/services/oauth"
)

func newGetContext(rawurl string) *web.Context {
	u, _ := url.Parse(rawurl)
	e := web.New()
	res := httptest.NewRecorder()
	req := httptest.NewRequest("GET", u.RequestURI(), nil)
	req.Host = u.Host

	if u.Scheme == "https" {
		req.TLS = &tls.ConnectionState{}
	}

	return web.NewContext(e, req, res, nil)
}

// ExpectAuthURL checks an authorization URL and the claims signed into its state parameter.
// The state can't be compared as a string because it carries a freshly generated expiry.
func ExpectAuthURL(actual, expectedPrefix, expectedRedirect, expectedIdentifier string) {
	Expect(strings.HasPrefix(actual, expectedPrefix)).IsTrue()

	claims, err := jwt.DecodeOAuthStateClaims(strings.TrimPrefix(actual, expectedPrefix))
	Expect(err).IsNil()
	Expect(claims.Redirect).Equals(expectedRedirect)
	Expect(claims.Identifier).Equals(expectedIdentifier)
	Expect(claims.ExpiresAt).IsNotNil()
}
func TestGetAuthURL_Facebook(t *testing.T) {
	RegisterT(t)
	bus.Init(&oauth.Service{})

	// Mock tenant provider status to return enabled by default
	bus.AddHandler(func(ctx context.Context, q *query.GetTenantProviderStatus) error {
		q.Result = &entity.TenantProvider{
			Provider:  q.Provider,
			IsEnabled: true,
		}
		return nil
	})

	ctx := newGetContext("http://login.test.fider.io:3000")
	authURL := &query.GetOAuthAuthorizationURL{
		Provider:   app.FacebookProvider,
		Redirect:   "http://example.org",
		Identifier: "456",
	}

	err := bus.Dispatch(ctx, authURL)
	Expect(err).IsNil()
	ExpectAuthURL(authURL.Result, "https://www.facebook.com/v3.2/dialog/oauth?client_id=FB_CL_ID&redirect_uri=http%3A%2F%2Flogin.test.fider.io%3A3000%2Foauth%2Ffacebook%2Fcallback&response_type=code&scope=public_profile+email&state=", "http://example.org", "456")
}

func TestGetAuthURL_Google(t *testing.T) {
	RegisterT(t)
	bus.Init(&oauth.Service{})

	// Mock tenant provider status to return enabled by default
	bus.AddHandler(func(ctx context.Context, q *query.GetTenantProviderStatus) error {
		q.Result = &entity.TenantProvider{
			Provider:  q.Provider,
			IsEnabled: true,
		}
		return nil
	})

	ctx := newGetContext("http://login.test.fider.io:3000")

	authURL := &query.GetOAuthAuthorizationURL{
		Provider:   app.GoogleProvider,
		Redirect:   "http://example.org",
		Identifier: "123",
	}

	err := bus.Dispatch(ctx, authURL)
	Expect(err).IsNil()
	ExpectAuthURL(authURL.Result, "https://accounts.google.com/o/oauth2/v2/auth?client_id=GO_CL_ID&redirect_uri=http%3A%2F%2Flogin.test.fider.io%3A3000%2Foauth%2Fgoogle%2Fcallback&response_type=code&scope=profile+email&state=", "http://example.org", "123")
}

func TestGetAuthURL_GitHub(t *testing.T) {
	RegisterT(t)
	bus.Init(&oauth.Service{})

	// Mock tenant provider status to return enabled by default
	bus.AddHandler(func(ctx context.Context, q *query.GetTenantProviderStatus) error {
		q.Result = &entity.TenantProvider{
			Provider:  q.Provider,
			IsEnabled: true,
		}
		return nil
	})

	ctx := newGetContext("http://login.test.fider.io:3000")

	authURL := &query.GetOAuthAuthorizationURL{
		Provider:   app.GitHubProvider,
		Redirect:   "http://example.org",
		Identifier: "456",
	}

	err := bus.Dispatch(ctx, authURL)
	Expect(err).IsNil()
	ExpectAuthURL(authURL.Result, "https://github.com/login/oauth/authorize?client_id=GH_CL_ID&redirect_uri=http%3A%2F%2Flogin.test.fider.io%3A3000%2Foauth%2Fgithub%2Fcallback&response_type=code&scope=user%3Aemail&state=", "http://example.org", "456")
}

func TestGetAuthURL_Custom(t *testing.T) {
	RegisterT(t)
	bus.Init(&oauth.Service{})

	bus.AddHandler(func(ctx context.Context, q *query.GetCustomOAuthConfigByProvider) error {
		if q.Provider == "_custom" {
			q.Result = &entity.OAuthConfig{
				Provider:     q.Provider,
				ClientID:     "CU_CL_ID",
				Scope:        "profile email",
				AuthorizeURL: "https://example.org/oauth/authorize",
			}
		}
		return nil
	})

	ctx := newGetContext("http://login.test.fider.io:3000")
	authURL := &query.GetOAuthAuthorizationURL{
		Provider:   "_custom",
		Redirect:   "http://example.org",
		Identifier: "456",
	}

	err := bus.Dispatch(ctx, authURL)
	Expect(err).IsNil()
	ExpectAuthURL(authURL.Result, "https://example.org/oauth/authorize?client_id=CU_CL_ID&redirect_uri=http%3A%2F%2Flogin.test.fider.io%3A3000%2Foauth%2F_custom%2Fcallback&response_type=code&scope=profile+email&state=", "http://example.org", "456")
}

func TestGetAuthURL_Twitch(t *testing.T) {
	RegisterT(t)
	bus.Init(&oauth.Service{})

	bus.AddHandler(func(ctx context.Context, q *query.GetCustomOAuthConfigByProvider) error {
		if q.Provider == "_custom" {
			q.Result = &entity.OAuthConfig{
				Provider:     q.Provider,
				ClientID:     "CU_CL_ID",
				Scope:        "openid",
				AuthorizeURL: "https://id.twitch.tv/oauth/authorize",
			}
		}
		return nil
	})

	ctx := newGetContext("http://login.test.fider.io:3000")
	authURL := &query.GetOAuthAuthorizationURL{
		Provider:   "_custom",
		Redirect:   "http://example.org",
		Identifier: "456",
	}

	err := bus.Dispatch(ctx, authURL)
	Expect(err).IsNil()
	ExpectAuthURL(authURL.Result, "https://id.twitch.tv/oauth/authorize?claims=%7B%22userinfo%22%3A%7B%22preferred_username%22%3Anull%2C%22email%22%3Anull%2C%22email_verified%22%3Anull%7D%7D&client_id=CU_CL_ID&redirect_uri=http%3A%2F%2Flogin.test.fider.io%3A3000%2Foauth%2F_custom%2Fcallback&response_type=code&scope=openid&state=", "http://example.org", "456")
}

func TestParseProfileResponse_AllFields(t *testing.T) {
	RegisterT(t)
	bus.Init(&oauth.Service{})

	bus.AddHandler(func(ctx context.Context, q *query.GetCustomOAuthConfigByProvider) error {
		if q.Provider == "_test1" {
			q.Result = &entity.OAuthConfig{
				Provider:          q.Provider,
				JSONUserIDPath:    "id",
				JSONUserNamePath:  "name",
				JSONUserEmailPath: "email",
			}
		}
		return nil
	})

	ctx := newGetContext("http://login.test.fider.io:3000")
	profile := &cmd.ParseOAuthRawProfile{
		Provider: "_test1",
		Body:     `{"name":"Jon Snow","email":"jon\u0040got.com","id":"789654"}`,
	}

	err := bus.Dispatch(ctx, profile)
	Expect(err).IsNil()

	Expect(profile.Result.ID).Equals("789654")
	Expect(profile.Result.Name).Equals("Jon Snow")
	Expect(profile.Result.Email).Equals("[email protected]")
}

func TestParseProfileResponse_WithoutEmail(t *testing.T) {
	RegisterT(t)
	bus.Init(&oauth.Service{})

	bus.AddHandler(func(ctx context.Context, q *query.GetCustomOAuthConfigByProvider) error {
		if q.Provider == "_test1" {
			q.Result = &entity.OAuthConfig{
				Provider:          q.Provider,
				JSONUserIDPath:    "id",
				JSONUserNamePath:  "name",
				JSONUserEmailPath: "email",
			}
		}
		return nil
	})

	ctx := newGetContext("http://login.test.fider.io:3000")
	profile := &cmd.ParseOAuthRawProfile{
		Provider: "_test1",
		Body:     `{"name":"Jon Snow","id":"1"}`,
	}
	err := bus.Dispatch(ctx, profile)
	Expect(err).IsNil()

	Expect(profile.Result.ID).Equals("1")
	Expect(profile.Result.Name).Equals("Jon Snow")
	Expect(profile.Result.Email).Equals("")
}

func TestParseProfileResponse_NestedData(t *testing.T) {
	RegisterT(t)
	bus.Init(&oauth.Service{})

	bus.AddHandler(func(ctx context.Context, q *query.GetCustomOAuthConfigByProvider) error {
		if q.Provider == "_test1" {
			q.Result = &entity.OAuthConfig{
				Provider:          q.Provider,
				JSONUserIDPath:    "id",
				JSONUserNamePath:  "profile.name",
				JSONUserEmailPath: "profile.email",
			}
		}
		return nil
	})

	ctx := newGetContext("http://login.test.fider.io:3000")
	profile := &cmd.ParseOAuthRawProfile{
		Provider: "_test1",
		Body: `{
			"id": "123",
			"profile": {
				"name": "Jon Snow",
				"email": "[email protected]"
			}
		}`,
	}
	err := bus.Dispatch(ctx, profile)
	Expect(err).IsNil()

	Expect(profile.Result.ID).Equals("123")
	Expect(profile.Result.Name).Equals("Jon Snow")
	Expect(profile.Result.Email).Equals("[email protected]")
}

func TestParseProfileResponse_WithFallback(t *testing.T) {
	RegisterT(t)
	bus.Init(&oauth.Service{})

	bus.AddHandler(func(ctx context.Context, q *query.GetCustomOAuthConfigByProvider) error {
		if q.Provider == "_test1" {
			q.Result = &entity.OAuthConfig{
				Provider:          q.Provider,
				JSONUserIDPath:    "id",
				JSONUserNamePath:  "profile.name, profile.login",
				JSONUserEmailPath: "profile.email",
			}
		}
		return nil
	})

	ctx := newGetContext("http://login.test.fider.io:3000")
	profile := &cmd.ParseOAuthRawProfile{
		Provider: "_test1",
		Body: `{
			"id": 123,
			"profile": {
				"login": "jonny",
				"email": "[email protected]"
			}
		}`,
	}
	err := bus.Dispatch(ctx, profile)
	Expect(err).IsNil()

	Expect(profile.Result.ID).Equals("123")
	Expect(profile.Result.Name).Equals("jonny")
	Expect(profile.Result.Email).Equals("[email protected]")
}

func TestParseProfileResponse_UseEmailWhenNameIsEmpty(t *testing.T) {
	RegisterT(t)
	bus.Init(&oauth.Service{})

	bus.AddHandler(func(ctx context.Context, q *query.GetCustomOAuthConfigByProvider) error {
		if q.Provider == "_test1" {
			q.Result = &entity.OAuthConfig{
				Provider:          q.Provider,
				JSONUserIDPath:    "id",
				JSONUserNamePath:  "profile.name",
				JSONUserEmailPath: "profile.email",
			}
		}
		return nil
	})

	ctx := newGetContext("http://login.test.fider.io:3000")
	profile := &cmd.ParseOAuthRawProfile{
		Provider: "_test1",
		Body: `{
			"id": "123",
			"profile": {
				"email": "[email protected]"
			}
		}`,
	}
	err := bus.Dispatch(ctx, profile)
	Expect(err).IsNil()

	Expect(profile.Result.ID).Equals("123")
	Expect(profile.Result.Name).Equals("jon")
	Expect(profile.Result.Email).Equals("[email protected]")
}

func TestParseProfileResponse_InvalidEmail(t *testing.T) {
	RegisterT(t)
	bus.Init(&oauth.Service{})

	bus.AddHandler(func(ctx context.Context, q *query.GetCustomOAuthConfigByProvider) error {
		if q.Provider == "_test1" {
			q.Result = &entity.OAuthConfig{
				Provider:          q.Provider,
				JSONUserIDPath:    "id",
				JSONUserNamePath:  "profile.name",
				JSONUserEmailPath: "profile.email",
			}
		}
		return nil
	})

	ctx := newGetContext("http://login.test.fider.io:3000")
	profile := &cmd.ParseOAuthRawProfile{
		Provider: "_test1",
		Body: `{
			"id": "AB123",
			"profile": {
				"name": "Jon Snow",
				"email": "jon"
			}
		}`,
	}
	err := bus.Dispatch(ctx, profile)
	Expect(err).IsNil()

	Expect(profile.Result.ID).Equals("AB123")
	Expect(profile.Result.Name).Equals("Jon Snow")
	Expect(profile.Result.Email).Equals("")
}

func TestParseProfileResponse_EmptyID(t *testing.T) {
	RegisterT(t)
	bus.Init(&oauth.Service{})

	bus.AddHandler(func(ctx context.Context, q *query.GetCustomOAuthConfigByProvider) error {
		if q.Provider == "_test1" {
			q.Result = &entity.OAuthConfig{
				Provider:          q.Provider,
				JSONUserIDPath:    "id",
				JSONUserNamePath:  "name",
				JSONUserEmailPath: "email",
			}
		}
		return nil
	})

	ctx := newGetContext("http://login.test.fider.io:3000")
	profile := &cmd.ParseOAuthRawProfile{
		Provider: "_test1",
		Body:     `{}`,
	}

	err := bus.Dispatch(ctx, profile)
	Expect(errors.Cause(err)).Equals(app.ErrUserIDRequired)
	Expect(profile.Result).IsNil()
}

func TestParseProfileResponse_EmptyName(t *testing.T) {
	RegisterT(t)
	bus.Init(&oauth.Service{})

	bus.AddHandler(func(ctx context.Context, q *query.GetCustomOAuthConfigByProvider) error {
		if q.Provider == "_test1" {
			q.Result = &entity.OAuthConfig{
				Provider:          q.Provider,
				JSONUserIDPath:    "id",
				JSONUserNamePath:  "name",
				JSONUserEmailPath: "email",
			}
		}
		return nil
	})

	ctx := newGetContext("http://login.test.fider.io:3000")
	profile := &cmd.ParseOAuthRawProfile{
		Provider: "_test1",
		Body:     `{ "id": "A0" }`,
	}

	err := bus.Dispatch(ctx, profile)
	Expect(err).IsNil()
	Expect(profile.Result.ID).Equals("A0")
	Expect(profile.Result.Name).Equals("Anonymous")
}

func TestCustomOAuth_Disabled(t *testing.T) {
	RegisterT(t)
	bus.Init(&oauth.Service{})

	bus.AddHandler(func(ctx context.Context, q *query.GetCustomOAuthConfigByProvider) error {
		if q.Provider == "_test1" {
			q.Result = &entity.OAuthConfig{
				Provider:          q.Provider,
				JSONUserIDPath:    "id",
				JSONUserNamePath:  "name",
				JSONUserEmailPath: "email",
				Status:            enum.OAuthConfigDisabled,
			}
		}
		return nil
	})

	ctx := newGetContext("http://login.test.fider.io:3000")
	rawProfile := &cmd.ParseOAuthRawProfile{
		Provider: "_test1",
		Body:     `{ "id": "A0", "name": "John" }`,
	}

	err := bus.Dispatch(ctx, rawProfile)
	Expect(err).IsNil()
	Expect(rawProfile.Result.ID).Equals("A0")
	Expect(rawProfile.Result.Name).Equals("John")

	oauthProfile := &query.GetOAuthProfile{
		Provider: "_test1",
	}

	err = bus.Dispatch(ctx, oauthProfile)
	Expect(err).IsNotNil()
	Expect(oauthProfile.Result).IsNil()
}

func TestParseOAuthRawProfile_CompositeName(t *testing.T) {
	RegisterT(t)

	// Initialize the OAuth service
	bus.Init(&oauth.Service{})

	testCases := []struct {
		name          string
		jsonBody      string
		jsonNamePath  string
		expectedName  string
		expectedEmail string
		expectedID    string
	}{
		{
			name:          "Simple path",
			jsonBody:      `{"id": "123", "name": "Jon Snow", "email": "[email protected]"}`,
			jsonNamePath:  "name",
			expectedName:  "Jon Snow",
			expectedEmail: "[email protected]",
			expectedID:    "123",
		},
		{
			name:          "Fallback path - first exists",
			jsonBody:      `{"id": "123", "name": "Jon Snow", "login": "jonsnow", "email": "[email protected]"}`,
			jsonNamePath:  "name, login",
			expectedName:  "Jon Snow",
			expectedEmail: "[email protected]",
			expectedID:    "123",
		},
		{
			name:          "Fallback path - first missing",
			jsonBody:      `{"id": "123", "login": "jonsnow", "email": "[email protected]"}`,
			jsonNamePath:  "name, login",
			expectedName:  "jonsnow",
			expectedEmail: "[email protected]",
			expectedID:    "123",
		},
		{
			name:          "Composite path with space",
			jsonBody:      `{"id": "123", "firstname": "Jon", "lastname": "Snow", "email": "[email protected]"}`,
			jsonNamePath:  "firstname + ' ' + lastname",
			expectedName:  "Jon Snow",
			expectedEmail: "[email protected]",
			expectedID:    "123",
		},
		{
			name:          "Composite path with comma",
			jsonBody:      `{"id": "123", "firstname": "Jon", "lastname": "Snow", "email": "[email protected]"}`,
			jsonNamePath:  "lastname + ', ' + firstname",
			expectedName:  "Snow, Jon",
			expectedEmail: "[email protected]",
			expectedID:    "123",
		},
		{
			name:          "Composite path with missing field",
			jsonBody:      `{"id": "123", "firstname": "Jon", "email": "[email protected]"}`,
			jsonNamePath:  "firstname + ' ' + lastname",
			expectedName:  "Jon", // lastname is missing, so only firstname is used
			expectedEmail: "[email protected]",
			expectedID:    "123",
		},
		{
			name:          "Nested JSON path",
			jsonBody:      `{"id": "123", "profile": {"name": {"first": "Jon", "last": "Snow"}}, "email": "[email protected]"}`,
			jsonNamePath:  "profile.name.first + ' ' + profile.name.last",
			expectedName:  "Jon Snow",
			expectedEmail: "[email protected]",
			expectedID:    "123",
		},
		{
			name:          "Empty name with email fallback",
			jsonBody:      `{"id": "123", "email": "[email protected]"}`,
			jsonNamePath:  "name",
			expectedName:  "jon.snow", // Should use part before @ in email
			expectedEmail: "[email protected]",
			expectedID:    "123",
		},
		{
			name:          "Empty name and email",
			jsonBody:      `{"id": "123"}`,
			jsonNamePath:  "name",
			expectedName:  "Anonymous", // Should use "Anonymous"
			expectedEmail: "",
			expectedID:    "123",
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			// Create a mock config for this test case
			mockConfig := &entity.OAuthConfig{
				JSONUserIDPath:    "id",
				JSONUserNamePath:  tc.jsonNamePath,
				JSONUserEmailPath: "email",
			}

			// Register a mock handler for getConfig
			bus.AddHandler(func(ctx context.Context, q *query.GetCustomOAuthConfigByProvider) error {
				q.Result = mockConfig
				return nil
			})

			// Create the parse command
			parseCmd := &cmd.ParseOAuthRawProfile{
				Provider: "test_provider",
				Body:     tc.jsonBody,
			}

			// Execute the command
			err := bus.Dispatch(context.Background(), parseCmd)
			Expect(err).IsNil()

			// Verify the result
			profile := parseCmd.Result
			Expect(profile).IsNotNil()
			Expect(profile.ID).Equals(tc.expectedID)
			Expect(profile.Name).Equals(tc.expectedName)
			Expect(profile.Email).Equals(tc.expectedEmail)
		})
	}
}

// Test for invalid inputs
func TestParseOAuthRawProfile_InvalidInputs(t *testing.T) {
	RegisterT(t)

	// Initialize the OAuth service
	bus.Init(&oauth.Service{})

	testCases := []struct {
		name          string
		jsonBody      string
		jsonNamePath  string
		expectedError bool
	}{
		{
			name:          "Missing ID",
			jsonBody:      `{"name": "Jon Snow", "email": "[email protected]"}`,
			jsonNamePath:  "name",
			expectedError: true,
		},
		{
			name:          "Invalid email",
			jsonBody:      `{"id": "123", "name": "Jon Snow", "email": "not-an-email"}`,
			jsonNamePath:  "name",
			expectedError: false, // Should not error, but email should be empty
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			// Create a mock config for this test case
			mockConfig := &entity.OAuthConfig{
				JSONUserIDPath:    "id",
				JSONUserNamePath:  tc.jsonNamePath,
				JSONUserEmailPath: "email",
			}

			// Register a mock handler for getConfig
			bus.AddHandler(func(ctx context.Context, q *query.GetCustomOAuthConfigByProvider) error {
				q.Result = mockConfig
				return nil
			})

			// Create the parse command
			parseCmd := &cmd.ParseOAuthRawProfile{
				Provider: "test_provider",
				Body:     tc.jsonBody,
			}

			// Execute the command
			err := bus.Dispatch(context.Background(), parseCmd)

			if tc.expectedError {
				Expect(err).IsNotNil()
			} else {
				Expect(err).IsNil()
				profile := parseCmd.Result
				Expect(profile).IsNotNil()
			}
		})
	}
}

func TestGetOAuthRawProfile_BlocksInternalTokenURL(t *testing.T) {
	RegisterT(t)
	bus.Init(&oauth.Service{})

	// Custom provider whose TokenURL targets an internal/link-local address.
	bus.AddHandler(func(ctx context.Context, q *query.GetCustomOAuthConfigByProvider) error {
		q.Result = &entity.OAuthConfig{
			Provider:       q.Provider,
			Status:         enum.OAuthConfigEnabled,
			ClientID:       "client",
			ClientSecret:   "secret",
			AuthorizeURL:   "https://example.org/authorize",
			TokenURL:       "http://169.254.169.254/latest/api/token",
			ProfileURL:     "https://example.org/profile",
			JSONUserIDPath: "id",
		}
		return nil
	})

	ctx := newGetContext("http://login.test.fider.io:3000")
	rawProfile := &query.GetOAuthRawProfile{Provider: "_test1", Code: "any-code"}

	// The SSRF guard must reject before any server-side request (no token exchange happens).
	err := bus.Dispatch(ctx, rawProfile)
	Expect(err).IsNotNil()
	Expect(rawProfile.Result).Equals("")
}