lib/utilities.js
function getIdentity(urlStr, urlTemplates, www) {
     var identity = {};

     urlObj = urlParser.parse(urlStr);
        
     // Loop all the urlMappings for site object
     for(var y = 0; y <= urlTemplates.length-1; y++){
        urlTemplate = urlTemplates[y];

        // remove http protocol    
        urlTemplate = removeHttpHttps(urlTemplate);
        
        // if the urlTemplate contains a username or userid parse it
        if (urlTemplate != '' && (urlTemplate.indexOf('{username}') > -1 || urlTemplate.indexOf('{userid}') > -1)) {
            
            // break up url template
            var parts = urlTemplate.split(/\{userid\}|\{username\}/),
                startMatch = false,
                endMatch = false,
                user = urlObj.href

            // remove protocol, querystring and fragment
            user = user.replace(urlObj.hash, '');
            user = user.replace(urlObj.search, ''); 
            user = removeHttpHttps(user); 
            

            // remove www if subdomain is optional
            if(www){
                user = user.replace('www.', '') ;
                urlStr = urlStr.replace('www.', '') ;
            }

            // remove any trailing /
            if (endsWith(user,'/'))
                user = user.substring(0, user.length - 1); 
              

            // remove unwanted front section of url string
            if (user.indexOf(parts[0]) === 0){
                startMatch = true;
                part = parts[0];
                user = user.substring(part.length, user.length);
            }
            
            // remove unwanted end section of url string
            if (parts.length === 2){
                // if no end part or its just and trailing /
                if (parts[1].length > 0 && parts[1] !== '/'){
                    // end part matches template
                    if (endsWith(user,parts[1])){
                        endMatch = true;
                        user = user.replace(parts[1], '');
                    } else if (endsWith(user,parts[1] + '/')) {
                        // end part matches template with a trailing /
                        endMatch = true;
                        user = user.replace(parts[1] + '/', '');
                    }
                } else {
                    endMatch = true;
                }
            }
            


            // if the user contain anymore / then do not use it
            if (user.indexOf("/") > -1)
                endMatch = false;


            if (startMatch && endMatch){
                
                identity = {}; 
                identity.domain = urlObj.host; 
                identity.matchedUrl = urlStr;

                if (urlTemplate.indexOf("{username}") > -1){
                    identity.userName = user;
                    identity.sgn = "sgn://" + urlObj.host + "/?ident=" + user;
                }

                if (urlTemplate.indexOf("{userid}") > -1){
                    identity.userId = user;
                    identity.sgn = "sgn://" + urlObj.host + "/?pk=" + user;
                }

                break;
            }                       
        } 
    } 

    return identity;
}
function getIdentity(urlStr, urlTemplates, www) {
     var identity = {};

     urlObj = urlParser.parse(urlStr);
        
     // Loop all the urlMappings for site object
     for(var y = 0; y <= urlTemplates.length-1; y++){
        urlTemplate = urlTemplates[y];

        // remove http protocol    
        urlTemplate = removeHttpHttps(urlTemplate);
        
        // if the urlTemplate contains a username or userid parse it
        if (urlTemplate != '' && (urlTemplate.indexOf('{username}') > -1 || urlTemplate.indexOf('{userid}') > -1)) {
            
            // break up url template
            var parts = urlTemplate.split(/\{userid\}|\{username\}/),
                startMatch = false,
                endMatch = false,
                user = urlObj.href

            // remove protocol, querystring and fragment
            user = user.replace(urlObj.hash, '');
            user = user.replace(urlObj.search, ''); 
            user = removeHttpHttps(user); 
            

            // remove www if subdomain is optional
            if(www){
                user = user.replace('www.', '') ;
                urlStr = urlStr.replace('www.', '') ;
            }

            // remove any trailing /
            if (endsWith(user,'/'))
                user = user.substring(0, user.length - 1); 
              

            // remove unwanted front section of url string
            if (user.indexOf(parts[0]) === 0){
                startMatch = true;
                part = parts[0];
                user = user.substring(part.length, user.length);
            }
            
            // remove unwanted end section of url string
            if (parts.length === 2){
                // if no end part or its just and trailing /
                if (parts[1].length > 0 && parts[1] !== '/'){
                    // end part matches template
                    if (endsWith(user,parts[1])){
                        endMatch = true;
                        user = user.replace(parts[1], '');
                    } else if (endsWith(user,parts[1] + '/')) {
                        // end part matches template with a trailing /
                        endMatch = true;
                        user = user.replace(parts[1] + '/', '');
                    }
                } else {
                    endMatch = true;
                }
            }
            


            // if the user contain anymore / then do not use it
            if (user.indexOf("/") > -1)
                endMatch = false;


            if (startMatch && endMatch){
                
                identity = {}; 
                identity.domain = urlObj.host; 
                identity.matchedUrl = urlStr;

                if (urlTemplate.indexOf("{username}") > -1){
                    identity.userName = user;
                    identity.sgn = "sgn://" + urlObj.host + "/?ident=" + user;
                }

                if (urlTemplate.indexOf("{userid}") > -1){
                    identity.userId = user;
                    identity.sgn = "sgn://" + urlObj.host + "/?pk=" + user;
                }

                break;
            }                       
        } 
    } 

    return identity;
}
function getIdentity ( urlStr , urlTemplates , www ) { var identity = { } ; urlObj = urlParser . parse ( urlStr ) ; // Loop all the urlMappings for site object for ( var y = 0 ; y <= urlTemplates . length - 1 ; y ++ ) { urlTemplate = urlTemplates [ y ] ; // remove http protocol     urlTemplate = removeHttpHttps ( urlTemplate ) ; // if the urlTemplate contains a username or userid parse it if ( urlTemplate != '' && ( urlTemplate . indexOf ( '{username}' ) > - 1 || urlTemplate . indexOf ( '{userid}' ) > - 1 ) ) { // break up url template var parts = urlTemplate . split ( / \{userid\}|\{username\} / ) , startMatch = false , endMatch = false , user = urlObj . href // remove protocol, querystring and fragment user = user . replace ( urlObj . hash , '' ) ; user = user . replace ( urlObj . search , '' ) ; user = removeHttpHttps ( user ) ; // remove www if subdomain is optional if ( www ) { user = user . replace ( 'www.' , '' ) ; urlStr = urlStr . replace ( 'www.' , '' ) ; } // remove any trailing / if ( endsWith ( user , '/' ) ) user = user . substring ( 0 , user . length - 1 ) ; // remove unwanted front section of url string if ( user . indexOf ( parts [ 0 ] ) === 0 ) { startMatch = true ; part = parts [ 0 ] ; user = user . substring ( part . length , user . length ) ; } // remove unwanted end section of url string if ( parts . length === 2 ) { // if no end part or its just and trailing / if ( parts [ 1 ] . length > 0 && parts [ 1 ] !== '/' ) { // end part matches template if ( endsWith ( user , parts [ 1 ] ) ) { endMatch = true ; user = user . replace ( parts [ 1 ] , '' ) ; } else if ( endsWith ( user , parts [ 1 ] + '/' ) ) { // end part matches template with a trailing / endMatch = true ; user = user . replace ( parts [ 1 ] + '/' , '' ) ; } } else { endMatch = true ; } } // if the user contain anymore / then do not use it if ( user . indexOf ( "/" ) > - 1 ) endMatch = false ; if ( startMatch && endMatch ) { identity = { } ; identity . domain = urlObj . host ; identity . matchedUrl = urlStr ; if ( urlTemplate . indexOf ( "{username}" ) > - 1 ) { identity . userName = user ; identity . sgn = "sgn://" + urlObj . host + "/?ident=" + user ; } if ( urlTemplate . indexOf ( "{userid}" ) > - 1 ) { identity . userId = user ; identity . sgn = "sgn://" + urlObj . host + "/?pk=" + user ; } break ; } } } return identity ; }
find the sgn and builds the identity object
find the sgn and builds the identity object