Skip to content

Commit

Permalink
feat: add default user mapping in custom oauth2 provider (#2819)
Browse files Browse the repository at this point in the history
  • Loading branch information
leo220yuyaodog authored Mar 18, 2024
1 parent ae1634a commit 44ae765
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 9 additions & 1 deletion idp/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,19 @@ func (idp *CustomIdProvider) GetUserInfo(token *oauth2.Token) (*UserInfo, error)
return nil, err
}

requiredFields := []string{"id", "username", "displayName"}
for _, field := range requiredFields {
_, ok := idp.UserMapping[field]
if !ok {
return nil, fmt.Errorf("cannot find %s in userMapping, please check your configuration in custom provider", field)
}
}

// map user info
for k, v := range idp.UserMapping {
_, ok := dataMap[v]
if !ok {
return nil, fmt.Errorf("cannot find %s in user from castom provider", v)
return nil, fmt.Errorf("cannot find %s in user from custom provider", v)
}
dataMap[k] = dataMap[v]
}
Expand Down
18 changes: 17 additions & 1 deletion web/src/ProviderEditPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ require("codemirror/mode/css/css");
const {Option} = Select;
const {TextArea} = Input;

const defaultUserMapping = {
id: "id",
username: "username",
displayName: "displayName",
email: "email",
avatarUrl: "avatarUrl",
};

class ProviderEditPage extends React.Component {
constructor(props) {
super(props);
Expand Down Expand Up @@ -70,7 +78,7 @@ class ProviderEditPage extends React.Component {

if (res.status === "ok") {
const provider = res.data;
provider.userMapping = provider.userMapping || {};
provider.userMapping = provider.userMapping || defaultUserMapping;
this.setState({
provider: provider,
});
Expand Down Expand Up @@ -141,8 +149,16 @@ class ProviderEditPage extends React.Component {
}

updateUserMappingField(key, value) {
const requiredKeys = ["id", "username", "displayName"];
const provider = this.state.provider;

if (value === "" && requiredKeys.includes(key)) {
Setting.showMessage("error", i18next.t("provider:This field is required"));
return;
}

provider.userMapping[key] = value;

this.setState({
provider: provider,
});
Expand Down

0 comments on commit 44ae765

Please sign in to comment.