Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Return service from findCredentials invocation #290

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Find all accounts and password for the `service` in the keychain.

`service` - The string service name.

Yields an array of `{ account: 'foo', password: 'bar' }`.
Yields an array of `{ service: 'foo', account: 'bar', password: 'baz' }`.

### findPassword(service)

Expand Down
2 changes: 1 addition & 1 deletion keytar.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ export declare function findPassword(service: string): Promise<string | null>;
*
* @returns A promise for the array of found credentials.
*/
export declare function findCredentials(service: string): Promise<Array<{ account: string, password: string}>>;
export declare function findCredentials(service: string): Promise<Array<{ service: string, account: string, password: string}>>;
10 changes: 8 additions & 2 deletions spec/keytar-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ describe("keytar", function() {
return a.account.localeCompare(b.account)
})

assert.deepEqual([{account: account, password: password}, {account: account2, password: password2}], sorted)
assert.deepEqual([
{ service: service, account: account, password: password },
{ service: service, account: account2, password: password2 }
], sorted)
});

it('returns an empty array when no credentials are found', async function() {
Expand Down Expand Up @@ -191,7 +194,10 @@ describe("keytar", function() {
return a.account.localeCompare(b.account)
})

assert.deepEqual([{account: account2, password: password2}, {account: account, password: password}], sorted)
assert.deepEqual([
{ service: service, account: account2, password: password2 },
{ service: service, account: account, password: password }
], sorted)
})

afterEach(async function() {
Expand Down
16 changes: 12 additions & 4 deletions src/async.cc
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,24 @@ void FindCredentialsWorker::OnOK() {
keytar::Credentials cred = *it;
Napi::Object obj = Napi::Object::New(env);

Napi::String service = Napi::String::New(env,
std::get<0>(cred).data(),
std::get<0>(cred).length());

Napi::String account = Napi::String::New(env,
cred.first.data(),
cred.first.length());
std::get<1>(cred).data(),
std::get<1>(cred).length());

Napi::String password = Napi::String::New(env,
cred.second.data(),
cred.second.length());
std::get<2>(cred).data(),
std::get<2>(cred).length());

#ifndef _WIN32
#pragma GCC diagnostic ignored "-Wunused-result"
#endif
obj.Set("service", service);
#ifndef _WIN32
#pragma GCC diagnostic ignored "-Wunused-result"
#endif
obj.Set("account", account);
#ifndef _WIN32
Expand Down
4 changes: 2 additions & 2 deletions src/credentials.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#define SRC_CREDENTIALS_H_

#include <string>
#include <utility>
#include <tuple>

namespace keytar {

typedef std::pair<std::string, std::string> Credentials;
typedef std::tuple<std::string, std::string, std::string> Credentials;

}

Expand Down
1 change: 1 addition & 0 deletions src/keytar_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ Credentials getCredentialsForItem(CFDictionaryRef item) {
kCFStringEncodingUTF8);

Credentials cred = Credentials(
CFStringToStdString(service),
CFStringToStdString(account),
CFStringToStdString(password));
CFRelease(password);
Expand Down
2 changes: 1 addition & 1 deletion src/keytar_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ KEYTAR_OP_RESULT FindCredentials(const std::string& service,
continue;
}

credentials->push_back(Credentials(account, password));
credentials->push_back(Credentials(service, account, password));
free(account);
free(password);
}
Expand Down
2 changes: 1 addition & 1 deletion src/keytar_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ KEYTAR_OP_RESULT FindCredentials(const std::string& service,
cred->CredentialBlob),
cred->CredentialBlobSize);

credentials->push_back(Credentials(login, password));
credentials->push_back(Credentials(service, login, password));
}

CredFree(creds);
Expand Down