Skip to content

Commit

Permalink
Add maskEmail(), fix Safari bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
hsluoyz committed Jun 25, 2021
1 parent 234a9b9 commit 2ba4474
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
3 changes: 2 additions & 1 deletion util/regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import (
var rePhoneCn *regexp.Regexp

func init() {
rePhoneCn, _ = regexp.Compile("^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|191|198|199|(147))\\d{8}$")
// https://learnku.com/articles/31543
rePhoneCn, _ = regexp.Compile("^1(3\\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\\d|9[0-35-9])\\d{8}$")
}

func IsEmailValid(email string) bool {
Expand Down
33 changes: 30 additions & 3 deletions web/src/Setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export let ServerUrl = "";
// export const StaticBaseUrl = "https://cdn.jsdelivr.net/gh/casbin/static";
export const StaticBaseUrl = "https://cdn.casbin.org";

// reference link: https://github.com/yiminghe/async-validator/blob/057b0b047f88fac65457bae691d6cb7c6fe48ce1/src/rule/type.ts#L9
export const EmailRegEx = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
// https://github.com/yiminghe/async-validator/blob/057b0b047f88fac65457bae691d6cb7c6fe48ce1/src/rule/type.ts#L9
export const EmailRegEx = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

// reference link: https://learnku.com/articles/31543, `^s*$` filter empty email individually.
// https://learnku.com/articles/31543, `^s*$` filter empty email individually.
export const PhoneRegEx = /^\s*$|^1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$/;

export function initServerUrl() {
Expand Down Expand Up @@ -420,3 +420,30 @@ export function getLabel(text, tooltip) {
</React.Fragment>
);
}

function repeat(str, len) {
while (str.length < len) {
str += str.substr(0, len - str.length);
}
return str;
}

function maskString(s) {
if (s.length <= 2) {
return s;
} else {
return `${s[0]}${repeat("*", s.length - 2)}${s[s.length - 1]}`;
}
}

export function maskEmail(email) {
const tokens = email.split("@");
let username = tokens[0];
username = maskString(username);

const domain = tokens[1];
let domainTokens = domain.split(".");
domainTokens[domainTokens.length - 2] = maskString(domainTokens[domainTokens.length - 2]);

return `${username}@${domainTokens.join(".")}`;
}
4 changes: 1 addition & 3 deletions web/src/auth/ForgetPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,7 @@ class ForgetPage extends React.Component {
{this.state.phone.replace(/(\d{3})\d*(\d{4})/,'$1****$2')}
</Option>
<Option key={2} value={this.state.email}>
{this.state.email.split("@")[0].length>2?
this.state.email.replace(/(?<=.)[^@]+(?=.@)/, "*****"):
this.state.email.replace(/(\w?@)/, "*@")}
{Setting.maskEmail(this.state.email)}
</Option>
</Select>
}
Expand Down

0 comments on commit 2ba4474

Please sign in to comment.