feat: add default user mapping in custom oauth2 provider (#2819)

This commit is contained in:
Yaodong Yu
2024-03-18 23:01:17 +08:00
committed by GitHub
parent ae1634a4d5
commit 44ae76503e
2 changed files with 26 additions and 2 deletions

View File

@ -98,11 +98,19 @@ func (idp *CustomIdProvider) GetUserInfo(token *oauth2.Token) (*UserInfo, error)
return nil, err 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 // map user info
for k, v := range idp.UserMapping { for k, v := range idp.UserMapping {
_, ok := dataMap[v] _, ok := dataMap[v]
if !ok { 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] dataMap[k] = dataMap[v]
} }

View File

@ -40,6 +40,14 @@ require("codemirror/mode/css/css");
const {Option} = Select; const {Option} = Select;
const {TextArea} = Input; const {TextArea} = Input;
const defaultUserMapping = {
id: "id",
username: "username",
displayName: "displayName",
email: "email",
avatarUrl: "avatarUrl",
};
class ProviderEditPage extends React.Component { class ProviderEditPage extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
@ -70,7 +78,7 @@ class ProviderEditPage extends React.Component {
if (res.status === "ok") { if (res.status === "ok") {
const provider = res.data; const provider = res.data;
provider.userMapping = provider.userMapping || {}; provider.userMapping = provider.userMapping || defaultUserMapping;
this.setState({ this.setState({
provider: provider, provider: provider,
}); });
@ -141,8 +149,16 @@ class ProviderEditPage extends React.Component {
} }
updateUserMappingField(key, value) { updateUserMappingField(key, value) {
const requiredKeys = ["id", "username", "displayName"];
const provider = this.state.provider; 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; provider.userMapping[key] = value;
this.setState({ this.setState({
provider: provider, provider: provider,
}); });