Skip to content

Commit

Permalink
TUSK_URLS are considered (#87)
Browse files Browse the repository at this point in the history
* TUSK_URLS are considered

* Update rEADME
  • Loading branch information
subdavis authored Feb 1, 2018
1 parent 3345061 commit 33b342a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 52 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

**Chrome:** https://chrome.google.com/webstore/detail/fmhmiaejopepamlcjkncpgpdjichnecm

## Tusk Custom Fields

* `TUSK_URLS`: a comma separated list of URLs or hostnames to which the entry should always match. For example, active directory login credentials that must match several sites. e.g. `my.login.domain.com,https://github.com,foobar.net`

## Build Setup

Tusk requires:
Expand Down
20 changes: 20 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@ const urlencode = function(str) {
return encodeURIComponent(str).replace(/[!'()*]/g, escape);
}

const getValidTokens = tokenString => {
if (!tokenString)
return []
else
return tokenString.toLowerCase().split(/\.|\s|\//).filter(t => {
return (t && t !== "com" && t !== "www" && t.length > 1)
})
} // end getValidTokens

const parseUrl = url => {
if (url && !url.indexOf('http') == 0)
url = 'http://' + url
//from https://gist.github.com/jlong/2428561
var parser = document.createElement('a')
parser.href = url
return parser
} // end parseUrl

export {
getValidTokens,
parseUrl,
urlencode
}
10 changes: 8 additions & 2 deletions services/googleDrivePasswordFileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,16 @@ function GoogleDrivePasswordFileManager(settings) {
}

// If this browser has the getAuthToken function. Hack for #64
if (chrome.identity.getAuthToken !== undefined){
oauth['auth'] = chrome_auth;
try {
if (chrome.identity.getAuthToken !== undefined){
oauth['auth'] = chrome_auth;
}
}
catch (e) {
console.info("Firefox mobile detected.")
}


return OauthManager(settings, oauth)
}

Expand Down
44 changes: 41 additions & 3 deletions services/keepassService.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ let Case = require('case'),
pako = require('pako'),
kdbxweb = require('kdbxweb')

import {
argon2
} from '$lib/argon2.js'
import { argon2 } from '$lib/argon2.js'
import { parseUrl, getValidTokens } from '$lib/utils.js'

function KeepassService(keepassHeader, settings, passwordFileStoreRegistry, keepassReference) {
var my = {};
Expand Down Expand Up @@ -68,6 +67,45 @@ function KeepassService(keepassHeader, settings, passwordFileStoreRegistry, keep
});
}

my.rankEntries = (entries, siteUrl, title, siteTokens) => {
entries.forEach(function(entry) {
//apply a ranking algorithm to find the best matches
var entryHostnames = [ parseUrl(entry.url).hostname || "" ]

if (entry.keys.indexOf('tuskUrls') >= 0){
let others = entry.tuskUrls
.split(',')
.map(val => {
return parseUrl(val).hostname
})
entryHostnames = entryHostnames.concat(others)
}

if (entryHostnames.length && entryHostnames.some((host => host == siteUrl.hostname)))
entry.matchRank = 100 //exact url match
else
entry.matchRank = 0

entry.matchRank += (entry.title && title && entry.title.toLowerCase() == title.toLowerCase()) ? 1 : 0
entry.matchRank += (entry.title && entry.title.toLowerCase() === siteUrl.hostname.toLowerCase()) ? 1 : 0
entry.matchRank += (entry.url && siteUrl.hostname.indexOf(entry.url.toLowerCase()) > -1) ? 0.9 : 0
entry.matchRank += (entry.title && siteUrl.hostname.indexOf(entry.title.toLowerCase()) > -1) ? 0.9 : 0

var entryTokens = getValidTokens(entryHostnames.join('.') + "." + entry.title);
for (var i = 0; i < entryTokens.length; i++) {
var token1 = entryTokens[i]
for (var j = 0; j < siteTokens.length; j++) {
var token2 = siteTokens[j]
if (token1 == token2) {
console.log(token2)
entry.matchRank += 0.2;
}

}
}
})
} // end rankEntries

function getKey(isKdbx, masterPassword, fileKey) {
var creds = new kdbxweb.Credentials(kdbxweb.ProtectedValue.fromString(masterPassword), fileKey);
return creds.ready.then(() => {
Expand Down
50 changes: 3 additions & 47 deletions src/components/Unlock.vue
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
</template>

<script>
import { parseUrl, getValidTokens } from '$lib/utils.js'
import InfoCluster from '@/components/InfoCluster'
import EntryList from '@/components/EntryList'
import Spinner from 'vue-simple-spinner'
Expand Down Expand Up @@ -223,55 +225,10 @@
},
showResults(entries) {
let getValidTokens = tokenString => {
if (!tokenString)
return []
else
return tokenString.toLowerCase().split(/\.|\s|\//).filter(t => {
return (t && t !== "com" && t !== "www" && t.length > 1)
})
} // end getValidTokens
let parseUrl = url => {
if (url && !url.indexOf('http') == 0)
url = 'http://' + url
//from https://gist.github.com/jlong/2428561
var parser = document.createElement('a')
parser.href = url
return parser
} // end parseUrl
let rankEntries = (entries, siteUrl, title, siteTokens) => {
entries.forEach(function(entry) {
//apply a ranking algorithm to find the best matches
var entryHostName = parseUrl(entry.url).hostname || ""
if (entryHostName && entryHostName == siteUrl.hostname)
entry.matchRank = 100 //exact url match
else
entry.matchRank = 0
entry.matchRank += (entry.title && title && entry.title.toLowerCase() == title.toLowerCase()) ? 1 : 0
entry.matchRank += (entry.title && entry.title.toLowerCase() === siteUrl.hostname.toLowerCase()) ? 1 : 0
entry.matchRank += (entry.url && siteUrl.hostname.indexOf(entry.url.toLowerCase()) > -1) ? 0.9 : 0
entry.matchRank += (entry.title && siteUrl.hostname.indexOf(entry.title.toLowerCase()) > -1) ? 0.9 : 0
var entryTokens = getValidTokens(entryHostName + "." + entry.title);
for (var i = 0; i < entryTokens.length; i++) {
var token1 = entryTokens[i]
for (var j = 0; j < siteTokens.length; j++) {
var token2 = siteTokens[j]
entry.matchRank += (token1 === token2) ? 0.2 : 0;
}
}
})
} // end rankEntries
let siteUrl = parseUrl(this.unlockedState.url)
let title = this.unlockedState.title
let siteTokens = getValidTokens(siteUrl.hostname + '.' + this.unlockedState.title)
rankEntries(entries, siteUrl, title, siteTokens) // in-place
this.keepassService.rankEntries(entries, siteUrl, title, siteTokens) // in-place
let allEntries = entries
let priorityEntries = entries
Expand Down Expand Up @@ -365,7 +322,6 @@
}
},
mounted() {
if (!this.isUnlocked()) {
let try_autounlock = () => {
Expand Down

0 comments on commit 33b342a

Please sign in to comment.