UNPKG

openui5-password

Version:

An OpenUI5 Control which checks password strength and validates it against predefined rules

6 lines 5.22 kB
/* * openui5-password * (c) Copyright 2017-2021 Mauricio Lauffer * Licensed under the MIT license. See LICENSE file in the project root for full license information. */ sap.ui.define(["sap/m/InputBase","sap/m/List","sap/m/ResponsivePopover","sap/m/StandardListItem","sap/ui/core/ValueState","openui5/password/PasswordRenderer","openui5/password/thirdparty/zxcvbn"],function(e,t,r,o,s,i){"use strict";const n=e.extend("Password",{metadata:{library:"openui5.password",properties:{requireNumbers:{type:"boolean",group:"Behavior",defaultValue:true},requireLetters:{type:"boolean",group:"Behavior",defaultValue:true},requireSymbols:{type:"boolean",group:"Behavior",defaultValue:true},requireLowercase:{type:"boolean",group:"Behavior",defaultValue:true},requireUppercase:{type:"boolean",group:"Behavior",defaultValue:true},maxLength:{type:"int",group:"Behavior",defaultValue:0},minLength:{type:"int",group:"Behavior",defaultValue:0},score:{type:"int",group:"Behavior",defaultValue:0}},aggregations:{errorItems:{type:"sap.m.StandardListItem",multiple:true,singularName:"errorItem"}}},renderer:function(e,t){i.render(e,t)}});n.prototype.init=function(){e.prototype.init.call(this);this._resourceBundle=sap.ui.getCore().getLibraryResourceBundle("openui5.password")};n.prototype.exit=function(){e.prototype.exit.call(this);if(this._popover){this._popover.destroy();this._popover=null}};n.prototype.oninput=function(t){e.prototype.oninput.call(this,t);if(t.isMarked("invalid")){return}const r=this._calculateScore(this._$input.val());this._setStatus(r);this.setScore(r)};n.prototype.onfocusin=function(){e.prototype.onfocusin.apply(this,arguments);this.$().addClass("sapMInputFocused");if(this._popover){this._popover.close()}};n.prototype.onfocusout=function(){e.prototype.onfocusout.apply(this,arguments);this.$().removeClass("sapMInputFocused");this.closeValueStateMessage(this)};n.prototype.onsapfocusleave=function(){this._showPasswordErrors();e.prototype.onsapfocusleave.apply(this,arguments)};n.prototype.setWidth=function(t){return e.prototype.setWidth.call(this,t||"100%")};n.prototype.getWidth=function(){return this.getProperty("width")||"100%"};n.prototype._showPasswordErrors=function(){const e=this._getPasswordErrors(this._$input.val());if(e.length>0){this._getPopover(e).openBy(this);this.setValueState(s.Error)}else{this.setValueState(s.Success)}};n.prototype._getPopover=function(e){if(!this._popover){this._createPopover()}this._addPasswordErrorsToPopover(e||[]);return this._popover};n.prototype._createPopover=function(){this._popover=new r(this.getId()+"-popover",{title:this._resourceBundle.getText("PASSWORD_POPUP_TITLE"),placement:"Vertical",icon:"sap-icon://alert"});this._popover.addContent(new t({}));this.addDependent(this._popover)};n.prototype._addPasswordErrorsToPopover=function(e){const t=this._popover.getContent()[0];t.removeAllItems();t.destroyItems();e.forEach(function(e){t.addItem(e)})};n.prototype._getPasswordErrors=function(e){this.destroyAggregation("errorItems");const t=this;const r=[];let i=/\d/;if(this.getRequireNumbers()&&!i.test(e)){r.push(new o({title:this._resourceBundle.getText("PASSWORD_MUST_HAVE_NUMBER"),info:"[0-9]",infoState:s.Error}))}i=/[a-zA-Z]/;if(this.getRequireLetters()&&!i.test(e)){r.push(new o({title:this._resourceBundle.getText("PASSWORD_MUST_HAVE_LETTER"),info:"[a-z , A-Z]",infoState:s.Error}))}i=/[a-z]/;if(this.getRequireLowercase()&&!i.test(e)){r.push(new o({title:this._resourceBundle.getText("PASSWORD_MUST_HAVE_LOWERCASE_LETTER"),info:"[a-z]",infoState:s.Error}))}i=/[A-Z]/;if(this.getRequireUppercase()&&!i.test(e)){r.push(new o({title:this._resourceBundle.getText("PASSWORD_MUST_HAVE_UPPERCASE_LETTER"),info:"[A-Z]",infoState:s.Error}))}i=/\W/;if(this.getRequireSymbols()&&!i.test(e)){r.push(new o({title:this._resourceBundle.getText("PASSWORD_MUST_HAVE_SYMBOL"),info:"[!, @, #, $, %, &...]",infoState:s.Error}))}if(this.getMinLength()>0&&e.length<this.getMinLength()){r.push(new o({title:this._resourceBundle.getText("PASSWORD_MUST_HAVE_NOT_LESS"),info:this._resourceBundle.getText("PASSWORD_LIMIT_CHARACTERS",this.getMinLength()),infoState:s.Error}))}if(this.getMaxLength()>0&&e.length>this.getMaxLength()){r.push(new o({title:this._resourceBundle.getText("PASSWORD_MUST_HAVE_NOT_MORE"),info:this._resourceBundle.getText("PASSWORD_LIMIT_CHARACTERS",this.getMaxLength()),infoState:s.Error}))}r.forEach(function(e){t.addAggregation("errorItems",e,true)});return r};n.prototype._calculateScore=function(e){if(!e||e.length<1){return 0}return zxcvbn(e).score||0};n.prototype._setStatus=function(e){const t=this._getStatus(e);this.setValueState(t.state);this.setValueStateText(t.text)};n.prototype._getStatus=function(e){let t={state:s.None,text:""};switch(e){case 0:t={state:s.Error,text:this._resourceBundle.getText("PASSWORD_IS_VERY_WEAK")};break;case 1:t={state:s.Error,text:this._resourceBundle.getText("PASSWORD_IS_WEAK")};break;case 2:t={state:s.Warning,text:this._resourceBundle.getText("PASSWORD_IS_NOT_STRONG_ENOUGH")};break;case 3:t={state:s.Success,text:this._resourceBundle.getText("PASSWORD_IS_STRONG")};break;case 4:t={state:s.Success,text:this._resourceBundle.getText("PASSWORD_IS_VERY_STRONG")};break;default:break}return t};return n});