Skip to content
This repository has been archived by the owner on Feb 13, 2025. It is now read-only.

Commit

Permalink
Merge pull request #263 from Kinvey/MLIBZ-2399_clientid_auth
Browse files Browse the repository at this point in the history
MLIBZ-2399 clientid auth
  • Loading branch information
thomasconner authored Mar 13, 2018
2 parents 131ac16 + 7244ef3 commit 7dc5276
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
26 changes: 6 additions & 20 deletions src/core/identity/mic.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export class MobileIdentityConnect extends Identity {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
authType: AuthType.App,
authType: AuthType.Client,
url: url.format({
protocol: this.client.micProtocol,
host: this.client.micHost,
Expand All @@ -238,7 +238,8 @@ export class MobileIdentityConnect extends Identity {
client_id: clientId,
redirect_uri: redirectUri,
code: code
}
},
clientId: clientId
});
return request.execute().then(response => response.data);
}
Expand All @@ -249,7 +250,7 @@ export class MobileIdentityConnect extends Identity {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
authType: AuthType.App,
authType: AuthType.Client,
url: url.format({
protocol: this.client.micProtocol,
host: this.client.micHost,
Expand All @@ -261,30 +262,15 @@ export class MobileIdentityConnect extends Identity {
redirect_uri: redirectUri,
refresh_token: token
},
clientId: clientId,
properties: options.properties,
timeout: options.timeout
});
return request.execute().then(response => response.data);
}

logout(user, options = {}) {
const request = new KinveyRequest({
method: RequestMethod.GET,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
authType: AuthType.App,
url: url.format({
protocol: this.client.micProtocol,
host: this.client.micHost,
pathname: '/oauth/invalidate',
query: {
user: user._id
}
}),
properties: options.properties
});
return request.execute().then(response => response.data);
return Promise.resolve();
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/core/identity/mic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ describe('MobileIdentityConnect', () => {
'/oauth/token',
`grant_type=authorization_code&client_id=${client.appKey}&redirect_uri=${encodeURIComponent(redirectUri)}&code=${code}`
)
.basicAuth({
user: client.appKey,
pass: client.appSecret
})
.reply(200, token, {
'Content-Type': 'application/json; charset=utf-8'
});
Expand Down Expand Up @@ -325,6 +329,10 @@ describe('MobileIdentityConnect', () => {
'/oauth/token',
`grant_type=authorization_code&client_id=${encodeURIComponent(client.appKey+'.'+micId)}&redirect_uri=${encodeURIComponent(redirectUri)}&code=${code}`
)
.basicAuth({
user: client.appKey + '.' + micId,
pass: client.appSecret
})
.reply(200, token, {
'Content-Type': 'application/json; charset=utf-8'
});
Expand Down
26 changes: 24 additions & 2 deletions src/core/request/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export const AuthType = {
Default: 'Default',
Master: 'Master',
None: 'None',
Session: 'Session'
Session: 'Session',
Client: 'Client'
};
Object.freeze(AuthType);

Expand Down Expand Up @@ -76,6 +77,23 @@ const Auth = {
.catch(() => Auth.app(client));
},

client(client, clientId) {
if (!client.appKey || !client.appSecret) {
return Promise.reject(
new Error('Missing client appKey and/or appSecret'
+ ' Use Kinvey.initialize() to set the appKey and appSecret for the client.')
);
}
if (!clientId){
clientId = client.appKey;
}
return Promise.resolve({
scheme: 'Basic',
username: clientId,
password: client.appSecret
});
},

/**
* Authenticate through Master Secret.
*
Expand Down Expand Up @@ -160,6 +178,7 @@ export class KinveyRequest extends NetworkRequest {
this.properties = options.properties || new Properties();
this.skipBL = options.skipBL === true;
this.trace = options.trace === true;
this.clientId = options.clientId;
}

static execute(options, client, dataOnly = true) {
Expand Down Expand Up @@ -320,7 +339,7 @@ export class KinveyRequest extends NetworkRequest {

// Add or remove the Authorization header
if (this.authType) {
// Get the auth info based on the set AuthType
// Get the auth info based on the set AuthType
switch (this.authType) {
case AuthType.All:
promise = Auth.all(this.client);
Expand All @@ -331,6 +350,9 @@ export class KinveyRequest extends NetworkRequest {
case AuthType.Basic:
promise = Auth.basic(this.client);
break;
case AuthType.Client:
promise = Auth.client(this.client, this.clientId);
break;
case AuthType.Master:
promise = Auth.master(this.client);
break;
Expand Down

0 comments on commit 7dc5276

Please sign in to comment.