Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MLIBZ-2399 clientid auth #263

Merged
merged 2 commits into from
Mar 13, 2018
Merged
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
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