Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c8cf70a

Browse files
authoredJul 25, 2016
Merge pull request #1 from gotchazipc/master
0.7.0 release
2 parents 605e09d + c228e8a commit c8cf70a

36 files changed

+4245
-0
lines changed
 

‎.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,10 @@ jspm_packages
3535

3636
# Optional REPL history
3737
.node_repl_history
38+
39+
# ide
40+
.idea
41+
.workspace
42+
43+
# test stuffs
44+
html-test/webida-service-*

‎README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,28 @@
11
# webida-service-client
2+
23
Webida service client API, based on webida-restful-api spec & generated codes
4+
5+
## Build Howto
6+
7+
If you're building webida-based product or service, you may want to customize api spec & regenerate client sources & docs.
8+
9+
### Pre-requisites
10+
11+
- node.js
12+
- webpack
13+
14+
Just install webpack to global. (unlike grunt/gulp, local installation is not mandatory yet)
15+
``` shell
16+
npm install -g webpack
17+
```
18+
19+
### Running webpack
20+
Just run, with proper options. currently, default -p option is just enough.
21+
``` shell
22+
webpack -p
23+
```
24+
25+
## Contributing
26+
27+
The files built by webpack, service-client-bundle.js & map, can be used as AMD module. Since pre-built files are part of releasing process, do not send a pull request changing the bundles.
28+

‎build.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
3+
FILES=webida-service-client-bundle.js*
4+
5+
rm -f $FILES ./html-test/$FILES
6+
webpack -p --progress
7+
gzip -fkv $FILES
8+
cp -f $FILES ./html-test

‎html-test/test-main.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
define(['./webida-service-client-bundle'], function (webida) {
2+
'use strict';
3+
4+
var ACP = webida.AbstractCredentialProvider;
5+
6+
function SimpleCredentialProvider() {
7+
ACP.call(this);
8+
}
9+
10+
SimpleCredentialProvider.prototype = Object.create(ACP.prototype)
11+
SimpleCredentialProvider.prototype.constructor = SimpleCredentialProvider;
12+
SimpleCredentialProvider.prototype.getUserCredentialAsync = function(err) {
13+
if(err) {
14+
alert('prev auth error = ' + err);
15+
}
16+
var ret = { };
17+
ret.loginId = window.prompt('input login id');
18+
ret.loginPassword = window.prompt('input login password');
19+
return Promise.resolve(ret);
20+
};
21+
22+
var bootArgs = {
23+
serverUrl:'http://localhost:3355',
24+
workspaceId:'4effdd36-695b-4f26-a99c-95394dd182be'
25+
};
26+
//
27+
// webida.init( new SimpleCredentialProvider(), bootArgs);
28+
// webida.start().then( function() {
29+
// console.log('start done');
30+
// });
31+
32+
webida.auth.initAuth(null, null, null, function() {
33+
console.log('initAuth callback', arguments);
34+
})
35+
36+
});

‎html-test/test.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>API Bundle Test</title>
5+
<!-- data-main attribute tells require.js to load scripts/main.js after require.js loads. -->
6+
<script data-main="test-main" src="http://requirejs.org/docs/release/2.2.0/comments/require.js "></script>
7+
</head>
8+
<body>
9+
<h1>Logger Test</h1>
10+
</body>
11+
</html>

‎lib/AbstractCredentialProvider.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2012-2016 S-Core Co., Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* @file AbstractSocketClient.js
19+
* @since 1.7.0
20+
* @author jh1977.kim@samsung.com
21+
*/
22+
23+
// we don't want cyclic dependencies between common and TokenManager.
24+
// so, instead of requiring just ./common, we require all dependencies directly
25+
// the only instance of TokenManager is saved in common
26+
define([
27+
'./common'
28+
], function (
29+
common
30+
) {
31+
'use strict';
32+
33+
function AbstractCredentialProvider(name) {
34+
this.name = name || this.constructor.name;
35+
if (this.constructor.name === 'AbstractCredentialProvider') {
36+
throw new TypeError('AbstractCredentialProvider is abstract class');
37+
}
38+
this.logger = common.logger.child(this.name);
39+
}
40+
41+
AbstractCredentialProvider.prototype = {
42+
getUserCredentialAsync : function getUserCredentialAsync(previousError) {
43+
this.logger.warn('getUserCredentialAsync is not implemented ');
44+
this.logger.debug('prev auth error', previousError);
45+
return Promise.reject(new Error('not implemented'));
46+
}
47+
};
48+
49+
return AbstractCredentialProvider;
50+
});

‎lib/auth-service.js

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Copyright (c) 2012-2016 S-Core Co., Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* @file session-service.js
19+
* @since 1.7.0
20+
* @author jh1977.kim@samsung.com
21+
*/
22+
23+
// we don't want cyclic dependencies between common and TokenManager.
24+
// so, instead of requiring just ./common, we require all dependencies directly
25+
// the only instance of TokenManager is saved in common
26+
define([
27+
'./common',
28+
'./token-manager'
29+
], function (
30+
common,
31+
tokenManager
32+
) {
33+
'use strict';
34+
var logger = common.logger.child('auth-service');
35+
var BOGUS_LOGIN_ID = 'webida-service-client';
36+
var BOGUS_LOGIN_PASSWORD = 'bogus';
37+
var MAX_LOGIN_TRY = 5;
38+
39+
var authApi = new common.api.AuthApi();
40+
41+
var privates = {
42+
credentialProvider : null,
43+
getCredentialAsync: function getCredentialAsync(previousError) {
44+
var credential = new common.api.Credential(BOGUS_LOGIN_ID, BOGUS_LOGIN_PASSWORD);
45+
if (common.masterToken) {
46+
credential.masterToken = common.masterToken;
47+
return Promise.resolve(credential);
48+
} else {
49+
return privates.credentialProvider.getUserCredentialAsync(previousError)
50+
.then(function (userCred) {
51+
credential.loginPassword = userCred.loginPassword;
52+
credential.loginId = userCred.loginId;
53+
delete credential.masterToken;
54+
return credential;
55+
});
56+
}
57+
},
58+
59+
loginAsync: function loginAsync(credential) {
60+
var loginCount = 0;
61+
return new Promise( function(resolve, reject) {
62+
function loginCallback(err, data) {
63+
loginCount++;
64+
if (err) {
65+
if (err.statusCode === 401) {
66+
if (loginCount > MAX_LOGIN_TRY) {
67+
var msg = 'too many failures ' + loginCount +'/'+MAX_LOGIN_TRY;
68+
var err = new Error(msg);
69+
logger.error('gave up retry', err);
70+
reject(err);
71+
} else {
72+
return privates.getCredentialAsync(err)
73+
.then(function(credential) {
74+
// call stack will grow up
75+
authApi.login(credential, loginCallback);
76+
})
77+
.catch( function(e) {
78+
logger.error('cannot get login credential', e);
79+
reject(e);
80+
});
81+
}
82+
} else {
83+
logger.error('login failed with unexpected error', loginCount, err);
84+
reject(err);
85+
}
86+
} else {
87+
logger.debug('login success', data);
88+
resolve(data);
89+
}
90+
}
91+
authApi.login(credential, loginCallback);
92+
});
93+
}
94+
};
95+
96+
var publics = {
97+
98+
getTokenManager: function getTokenManager() {
99+
return tokenManager;
100+
},
101+
102+
createMasterTokenAsync: function createMasterTokenAsync(workspaceId) {
103+
return new Promise( function(resolve, reject) {
104+
authApi.issueToken('MASTER', {
105+
workspaceId:workspaceId
106+
}, function(err, result) {
107+
if (err) {
108+
reject(err);
109+
} else {
110+
resolve(result);
111+
}
112+
});
113+
})
114+
},
115+
116+
init: function init(credentialProvider) {
117+
privates.credentialProvider = credentialProvider;
118+
tokenManager.on('lost', function(error) {
119+
logger.error('should login again', error);
120+
});
121+
tokenManager.on('updated', function(token) {
122+
logger.debug('updated token', token);
123+
});
124+
},
125+
126+
start: function start() {
127+
return privates.getCredentialAsync()
128+
.then( function(credential) {
129+
return privates.loginAsync(credential)
130+
})
131+
.then( function(token) {
132+
tokenManager.updateAccessToken(token);
133+
logger.debug('service start done');
134+
});
135+
},
136+
137+
stop: function stop() {
138+
tokenManager.dispose();
139+
logger.debug('service stop done');
140+
return Promise.resolve();
141+
}
142+
143+
};
144+
145+
146+
return publics;
147+
});

0 commit comments

Comments
 (0)
Please sign in to comment.