Skip to content

Commit

Permalink
chore: migrate idp service
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed Apr 13, 2024
1 parent a777032 commit c373131
Show file tree
Hide file tree
Showing 37 changed files with 1,093 additions and 1,333 deletions.
20 changes: 10 additions & 10 deletions plugin/idp/oauth2/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ import (
"golang.org/x/oauth2"

"github.com/usememos/memos/plugin/idp"
"github.com/usememos/memos/store"
storepb "github.com/usememos/memos/proto/gen/store"
)

// IdentityProvider represents an OAuth2 Identity Provider.
type IdentityProvider struct {
config *store.IdentityProviderOAuth2Config
config *storepb.OAuth2Config
}

// NewIdentityProvider initializes a new OAuth2 Identity Provider with the given configuration.
func NewIdentityProvider(config *store.IdentityProviderOAuth2Config) (*IdentityProvider, error) {
func NewIdentityProvider(config *storepb.OAuth2Config) (*IdentityProvider, error) {
for v, field := range map[string]string{
config.ClientID: "clientId",
config.ClientId: "clientId",
config.ClientSecret: "clientSecret",
config.TokenURL: "tokenUrl",
config.UserInfoURL: "userInfoUrl",
config.TokenUrl: "tokenUrl",
config.UserInfoUrl: "userInfoUrl",
config.FieldMapping.Identifier: "fieldMapping.identifier",
} {
if v == "" {
Expand All @@ -42,13 +42,13 @@ func NewIdentityProvider(config *store.IdentityProviderOAuth2Config) (*IdentityP
// ExchangeToken returns the exchanged OAuth2 token using the given authorization code.
func (p *IdentityProvider) ExchangeToken(ctx context.Context, redirectURL, code string) (string, error) {
conf := &oauth2.Config{
ClientID: p.config.ClientID,
ClientID: p.config.ClientId,
ClientSecret: p.config.ClientSecret,
RedirectURL: redirectURL,
Scopes: p.config.Scopes,
Endpoint: oauth2.Endpoint{
AuthURL: p.config.AuthURL,
TokenURL: p.config.TokenURL,
AuthURL: p.config.AuthUrl,
TokenURL: p.config.TokenUrl,
AuthStyle: oauth2.AuthStyleInParams,
},
}
Expand All @@ -69,7 +69,7 @@ func (p *IdentityProvider) ExchangeToken(ctx context.Context, redirectURL, code
// UserInfo returns the parsed user information using the given OAuth2 token.
func (p *IdentityProvider) UserInfo(token string) (*idp.IdentityProviderUserInfo, error) {
client := &http.Client{}
req, err := http.NewRequest(http.MethodGet, p.config.UserInfoURL, nil)
req, err := http.NewRequest(http.MethodGet, p.config.UserInfoUrl, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to new http request")
}
Expand Down
52 changes: 26 additions & 26 deletions plugin/idp/oauth2/oauth2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,52 @@ import (
"github.com/stretchr/testify/require"

"github.com/usememos/memos/plugin/idp"
"github.com/usememos/memos/store"
storepb "github.com/usememos/memos/proto/gen/store"
)

func TestNewIdentityProvider(t *testing.T) {
tests := []struct {
name string
config *store.IdentityProviderOAuth2Config
config *storepb.OAuth2Config
containsErr string
}{
{
name: "no tokenUrl",
config: &store.IdentityProviderOAuth2Config{
ClientID: "test-client-id",
config: &storepb.OAuth2Config{
ClientId: "test-client-id",
ClientSecret: "test-client-secret",
AuthURL: "",
TokenURL: "",
UserInfoURL: "https://example.com/api/user",
FieldMapping: &store.FieldMapping{
AuthUrl: "",
TokenUrl: "",
UserInfoUrl: "https://example.com/api/user",
FieldMapping: &storepb.FieldMapping{
Identifier: "login",
},
},
containsErr: `the field "tokenUrl" is empty but required`,
},
{
name: "no userInfoUrl",
config: &store.IdentityProviderOAuth2Config{
ClientID: "test-client-id",
config: &storepb.OAuth2Config{
ClientId: "test-client-id",
ClientSecret: "test-client-secret",
AuthURL: "",
TokenURL: "https://example.com/token",
UserInfoURL: "",
FieldMapping: &store.FieldMapping{
AuthUrl: "",
TokenUrl: "https://example.com/token",
UserInfoUrl: "",
FieldMapping: &storepb.FieldMapping{
Identifier: "login",
},
},
containsErr: `the field "userInfoUrl" is empty but required`,
},
{
name: "no field mapping identifier",
config: &store.IdentityProviderOAuth2Config{
ClientID: "test-client-id",
config: &storepb.OAuth2Config{
ClientId: "test-client-id",
ClientSecret: "test-client-secret",
AuthURL: "",
TokenURL: "https://example.com/token",
UserInfoURL: "https://example.com/api/user",
FieldMapping: &store.FieldMapping{
AuthUrl: "",
TokenUrl: "https://example.com/token",
UserInfoUrl: "https://example.com/api/user",
FieldMapping: &storepb.FieldMapping{
Identifier: "",
},
},
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestIdentityProvider(t *testing.T) {
ctx := context.Background()

const (
testClientID = "test-client-id"
testClientId = "test-client-id"

Check warning on line 116 in plugin/idp/oauth2/oauth2_test.go

View workflow job for this annotation

GitHub Actions / go-static-checks

var-naming: const testClientId should be testClientID (revive)
testCode = "test-code"
testAccessToken = "test-access-token"
testSubject = "123456789"
Expand All @@ -132,12 +132,12 @@ func TestIdentityProvider(t *testing.T) {
s := newMockServer(t, testCode, testAccessToken, userInfo)

oauth2, err := NewIdentityProvider(
&store.IdentityProviderOAuth2Config{
ClientID: testClientID,
&storepb.OAuth2Config{
ClientId: testClientId,
ClientSecret: "test-client-secret",
TokenURL: fmt.Sprintf("%s/oauth2/token", s.URL),
UserInfoURL: fmt.Sprintf("%s/oauth2/userinfo", s.URL),
FieldMapping: &store.FieldMapping{
TokenUrl: fmt.Sprintf("%s/oauth2/token", s.URL),
UserInfoUrl: fmt.Sprintf("%s/oauth2/userinfo", s.URL),
FieldMapping: &storepb.FieldMapping{
Identifier: "sub",
DisplayName: "name",
Email: "email",
Expand Down
2 changes: 1 addition & 1 deletion proto/api/v2/idp_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ message IdentityProvider {

message IdentityProviderConfig {
oneof config {
OAuth2Config oauth2 = 1;
OAuth2Config oauth2_config = 1;
}
}

Expand Down
2 changes: 1 addition & 1 deletion proto/api/v2/storage_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ message Storage {
}

message StorageConfig {
oneof storage_config {
oneof config {
S3Config s3_config = 1;
}
}
Expand Down
2 changes: 1 addition & 1 deletion proto/gen/api/v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ Used internally for obfuscating the page token.

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| oauth2 | [OAuth2Config](#memos-api-v2-OAuth2Config) | | |
| oauth2_config | [OAuth2Config](#memos-api-v2-OAuth2Config) | | |



Expand Down
Loading

0 comments on commit c373131

Please sign in to comment.