Skip to content

Commit

Permalink
Merge pull request #11 from electricimp/develop
Browse files Browse the repository at this point in the history
v2.0.1
  • Loading branch information
Pavel Petroshenko committed Oct 19, 2018
2 parents 80275de + c3ac2c0 commit ea431fb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 30 deletions.
33 changes: 14 additions & 19 deletions OAuth2.agent.lib.nut
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ enum Oauth2DeviceFlowState {

// The class that introduces OAuth2 namespace
class OAuth2 {
static VERSION = "2.0.0";
static VERSION = "2.0.1";
}

// The class that represents OAuth 2.0 authorization flow
Expand Down Expand Up @@ -69,7 +69,7 @@ class OAuth2.JWTProfile {
_expiresAt = 0;

// Debug mode, records non-error events
_debug = true;
_debug = false;

// Client constructor.
// Parameters:
Expand Down Expand Up @@ -159,7 +159,7 @@ class OAuth2.JWTProfile {
crypto.sign(crypto.RSASSA_PKCS1_SHA256, header + "." + body, _decodePem(_jwtSignKey),
function(err, sig) {
if (err) {
server.error(err);
_error(err);
return;
}

Expand All @@ -169,8 +169,8 @@ class OAuth2.JWTProfile {
"assertion" : (header + "." + body + "." + signature)
});

server.log("Making a request to the host: " + _tokenHost);
server.log((header + "." + body + "." + signature));
_log("Making a request to the host: " + _tokenHost);
_log((header + "." + body + "." + signature));

// Post, get the token
local request = http.post(_tokenHost, {}, oauthreq);
Expand Down Expand Up @@ -215,7 +215,7 @@ class OAuth2.JWTProfile {
} else {
// Error getting token
local mess = "Error getting token: " + resp.statuscode + " " + resp.body;
client._error(mess);
client._log(mess);
userCallback(null, mess);
}
}
Expand Down Expand Up @@ -513,7 +513,7 @@ class OAuth2.DeviceFlow {
local respData = http.jsondecode(resp.body);
if (null != _extractPollData(respData)) {
_reset();
_error("Something went wrong during code request: " + resp.body);
_log("Something went wrong during code request: " + resp.body);
cb(null, resp.body);
return;
}
Expand All @@ -527,7 +527,7 @@ class OAuth2.DeviceFlow {
} catch (error) {
_reset();
local msg = "Provider data processing error: " + error;
_error(msg);
_log(msg);
cb(null, msg);
}
}
Expand All @@ -547,15 +547,15 @@ class OAuth2.DeviceFlow {
local respData = http.jsondecode(resp.body);
if (null != _extractToken(respData)) {
_reset();
_error("Something went wrong during refresh: " + resp.body);
_log("Something went wrong during refresh: " + resp.body);
cb(null, resp.body);
} else {
cb(_accessToken, null);
}
} catch (error) {
_reset();
local msg = "Token refreshing error: " + error
_error(msg);
local msg = "Token refreshing error: " + error;
_log(msg);
cb(null, msg);
}
}
Expand All @@ -579,7 +579,7 @@ class OAuth2.DeviceFlow {
if (date().time > _expiresAt) {
_reset();
local msg = "Token acquiring timeout";
_error(msg);
_log(msg);
cb(null, msg);
return msg;
}
Expand Down Expand Up @@ -642,14 +642,14 @@ class OAuth2.DeviceFlow {
}
} else {
local msg = "Unexpected server response code:" + statusCode;
_error(msg);
_log(msg);
_reset();
cb(null, msg);
}
} catch (error) {
local msg = "General server poll error: " + error;
_reset();
_error(msg);
_log(msg);
cb(null, msg);
}
}
Expand Down Expand Up @@ -792,11 +792,6 @@ class OAuth2.DeviceFlow {
}
}

// Records error event
function _error(txt) {
server.error("[OAuth2DeviceFlow] " + txt);
}

// Records non-error event
function _log(txt) {
if (_debug) {
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This library provides OAuth 2.0 authentication and authorization flows. It suppo

The library exposes retrieved access tokens for applications and hides provider-specific operations, including the renewal of expired tokens.

**To add this library to your project, add** `#require "OAuth2.agent.lib.nut:2.0.0"` **to the top of your agent code.**
**To add this library to your project, add** `#require "OAuth2.agent.lib.nut:2.0.1"` **to the top of your agent code.**

## OAuth2.JWTProfile.Client ##

Expand Down Expand Up @@ -38,7 +38,7 @@ The second parameter, *userSettings*, defines a table with user- and application

```squirrel
// OAuth 2.0 library
#require "OAuth2.agent.lib.nut:2.0.0"
#require "OAuth2.agent.lib.nut:2.0.1"
// Substitute with real values
const GOOGLE_ISS = "rsalambda@quick-cacao-168121.iam.gserviceaccount.com";
Expand Down Expand Up @@ -113,7 +113,7 @@ if (token) {

### isTokenValid() ###

This method checks if the access token is valid by comparing its expiry time with current time.
This method checks if the access token is valid by comparing its expiry time with current time.

#### Return Value ####

Expand All @@ -128,7 +128,7 @@ server.log("The access token is " + (client.isTokenValid() ? "" : "in") + "valid
### Complete Example ###

```squirrel
#require "OAuth2.agent.lib.nut:2.0.0
#require "OAuth2.agent.lib.nut:2.0.1
// Substitute with real values
const GOOGLE_ISS = "rsalambda@quick-cacao-168121.iam.gserviceaccount.com";
Expand Down Expand Up @@ -232,7 +232,7 @@ The *notifyUserCallback* function should have the following parameters:

#### Return Value ####

String — `null` in the case of success, or an error message if the client is already performing a request and the *force* directive is set.
String — `null` in the case of success, or an error message if the client is already performing a request and the *force* directive is set.

#### Example ####

Expand Down Expand Up @@ -305,7 +305,7 @@ server.log("The client is " + (client.isAuthorized() ? "" : "un") + "authorized

### refreshAccessToken(*tokenReadyCallback*) ###

This method asynchronously refreshes the access token and invokes the callback function when this has been completed or an error occurs.
This method asynchronously refreshes the access token and invokes the callback function when this has been completed or an error occurs.

#### Parameters ####

Expand Down Expand Up @@ -342,7 +342,7 @@ client.refreshAccessToken(
### Complete Example ###

```squirrel
#require "OAuth2.agent.lib.nut:2.0.0
#require "OAuth2.agent.lib.nut:2.0.1
// Fill CLIENT_ID and CLIENT_SECRET with correct values
local userConfig = { "clientId" : "<CLIENT_ID>",
Expand Down
2 changes: 1 addition & 1 deletion examples/DeviceFlowGoogle.agent.nut
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#require "OAuth2.agent.lib.nut:2.0.0"
#require "OAuth2.agent.lib.nut:2.0.1"

const CLIENT_ID = "";
const CLIENT_SECRET = "";
Expand Down
2 changes: 1 addition & 1 deletion examples/JWTGooglePubSub.agent.nut
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// OTHER DEALINGS IN THE SOFTWARE.

//@include "../OAuth2.agent.lib.nut"
#require "OAuth2.agent.lib.nut:2.0.0"
#require "OAuth2.agent.lib.nut:2.0.1"

const GOOGLE_ISS = "";
const GOOGLE_SECRET_KEY = "";
Expand Down
4 changes: 2 additions & 2 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Google JWT Profile Tests (PubSub Scope) ##

Follow the JWT example [instructions](examples#jwt-profile-for-oauth-20) to obtain the *GOOGLE_ISS* and *GOOGLE_SECRET_KEY* values and set them as the environment variables.
Follow the JWT example [instructions](../examples#jwt-profile-for-oauth-20) to obtain the *GOOGLE_ISS* and *GOOGLE_SECRET_KEY* values and set them as the environment variables.

This test suite can be run automatically:

Expand All @@ -12,7 +12,7 @@ impt test run --tests GooglePubSubJWTAuth.agent.test.nut::

## Google Device Flow Tests ##

Follow the OAuth 2.0 Device Flow example [instructions](examples#oauth-20-device-flow) to obtain the *CLIENT_ID* and *CLIENT_SECRET* values and set them as the environment variables.
Follow the OAuth 2.0 Device Flow example [instructions](../examples#oauth-20-device-flow) to obtain the *CLIENT_ID* and *CLIENT_SECRET* values and set them as the environment variables.

This test requires some manual interaction and can be run:

Expand Down

0 comments on commit ea431fb

Please sign in to comment.