Search This Blog

Friday, March 13, 2020

OAuth2 and Open ID connect

OAuth2 is a standard protocol for authorisation. It is a framework which delegates the user authentication to a service, which manages the user accounts. It provides flows for web, desktop and mobile applications.

https://oauth.net/2/

OpenID Connect is an extension of OAuth2. An OAuth2 server which implements OpenID connect is a so called OpenID provider (OP). The client of an OpenID connect server is called Relying Party (RP).
OpenID Connect offers the possibility to retrieve user profile information beside the access token defined within OAuth2. The user information is delivered within the payload of the id_token or within the access_token.
The following steps are the flow of the authorization code flow of an OP

  1. The RP open the app and clicks login
  2. The app starts an authorize request by opening the website which is defined within the authorization endpoint and specifies a redirect url
  3. The user fills in username and password or any information the OP needs to authorise it's user
  4. After the user click's continue on the login page the OP will redirect to the url specified in 2. and add an authorization code as a parameter to the redirect url
  5. The app fetches the authorization code and calls the token endpoint with the grant_type "authorization_code" to obtain an access token
  6. The OP will reply with an access token, refresh token and a lot of other field defined in oauth2 spec
  7. The app could now use the access token to authorize the logged in user
  8. Within the access token or as a separat id token the app could extract user profile information delivered by the OP

Tuesday, January 28, 2020

[ERROR] Unable to find suitable Xcode install

Problem
I was unable to build a titanium appcelerator based app on an iMac. My target was ios. So it seems to me that something must be missing within my xcode install.

Solution
Even if they are installed, configure your xcode cmd line tools. Go to

Xcode->Preferences->Locations

and check that the Command Line Tools are visible like in the following screen


Thursday, January 16, 2020

JUnit5 or TestNG

Both frameworks are full of testing features. The list of features is quite similar in both frameworks. Here are a few links you might check out the list of relevant features:


I choose TestNG because of a feature which JUnit5 is currently missing
Group Test
This feature let me group my i.e. integration test together and let them run in my master build. With TestNG I could also do a before or after group and initialise my test group or cleanup after group run.
With JUnit5 we got something called Tags. Tags are good for grouping tests together. Let's see if they implement more in this direction in future.

Thursday, May 17, 2018

spyOn static Methods in jasmine tests

If you want to spy on a static method within your jasmine tests, you could do a simple spy like


beforeAll(async() =>{
  spyOn(UserService, "staticMethod").and.returnValue(true);
});


I'll tend to do this within beforeAll to avoid having problems when doing this in beforeEach. Within beforeEach you might end up in error messages like 

Error: <spyOn> : staticMethod has already been spied upon
    at <Jasmine>
    at UserContext.<anonymous> src/app/shared/components/result-detail/sample.component.spec.ts:74:5)
    at ZoneDelegate.webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke node_modules/zone.js/dist/zone.js:388:1)
    at ProxyZoneSpec.webpackJsonp../node_modules/zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke node_modules/zone.js/dist/proxy.js:128:1)
    at ZoneDelegate.webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke node_modules/zone.js/dist/zone.js:387:1)
    at Zone.webpackJsonp../node_modules/zone.js/dist/zone.js.Zone.run node_modules/zone.js/dist/zone.js:138:1)
    at runInTestZone node_modules/zone.js/dist/jasmine-patch.js:145:1)
    at UserContext.<anonymous> node_modules/zone.js/dist/jasmine-patch.js:160:1)

Jasmine runner TypeError 'next' in Angular5 Tests

I stumbled upon an error, which a karma runner gives me in some of our jasmine tests:

TypeError: Cannot read property 'next' of undefined
    at <Jasmine>
    at Function.continuer node_modules/q/q.js:1278:1)
    at UserContext.<anonymous> node_modules/q/q.js:1305:1)
    at ZoneDelegate.webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke node_modules/zone.js/dist/zone.js:388:1)
    at ProxyZoneSpec.webpackJsonp../node_modules/zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke node_modules/zone.js/dist/proxy.js:128:1)
    at ZoneDelegate.webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke node_modules/zone.js/dist/zone.js:387:1)
    at Zone.webpackJsonp../node_modules/zone.js/dist/zone.js.Zone.run node_modules/zone.js/dist/zone.js:138:1)
    at runInTestZone node_modules/zone.js/dist/jasmine-patch.js:145:1)
    at UserContext.<anonymous> node_modules/zone.js/dist/jasmine-patch.js:160:1)
    at <Jasmine>

I couldn't find something wrong with the test itself, but I noticed an strange import:

[async] from "q";

This was the reason for the strange error message. Correct import ist

[async] from "@angular/core/testing";

Monday, March 5, 2018

How to transform a Array to a map in Javascript/Typescript

This might be an easy one but I thought it is worth saving it here.
Let's assume you want to create a HashMap in typescript or javascript with http status codes and corresponding messages. This might be useful within a frontend error handler.
Here is your error-messages.json


[
  {"code": 403,    "message": "You are not permitted to use this action."  },
  {"code": 400,    "message": "The request was incomplete or wrong."  }, 
  {"code": 500,    "message": "A general error has occured."  }
]

here is how you could use it in your code:


// Map with error codes
  errorCodeMapping: Map<number, string> = new Map<number, string>();
// read error codes as json map
let array = Array.from(require("./error-messages.json"));
// map array to errorCode Map
this.errorCodeMapping = new Map(array.map((i: ErrorMessage): [number, string] => [i.code, i.message]));
// get a message for an error code
let msg: string = this.errorCodeMapping.get(403);

Karma runner Disconnected (1 times), because no message in 10000 ms.

We are using karma as the test runner within our angular 5 application. It does a pretty good job when it's time to build regression tests for our frontend. After a while using it I noticed an error during the coverage test run.

> xxxx-frontend@0.0.0 coverage C:\workspaces\andre\Intellij\xxxx\frontend
> ng test --cc --single-run
05 03 2018 08:04:47.006:INFO [karma]: Karma v2.0.0 server started at http://0.0.0.0:9876/
05 03 2018 08:04:47.009:INFO [launcher]: Launching browser Chrome with unlimited concurrency
05 03 2018 08:04:47.016:INFO [launcher]: Starting browser Chrome
05 03 2018 08:05:06.109:INFO [Chrome 64.0.3282 (Windows 7.0.0)]: Connected on socket fwQIL6cCt7q6VM0sAAAA with id 76897689
05 03 2018 08:05:12.110:WARN [Chrome 64.0.3282 (Windows 7.0.0)]: Disconnected (1 times), because no message in 6000 ms.
Chrome 64.0.3282 (Windows 7.0.0) ERROR
 
Disconnected, because no message in 10000 ms.

Sometimes this happens because we got an error wihtin the test cases. After writing a lot of test cases I noticed that the execution of all test cases took about 11 sec. So I increased our timeout with setting

browserNoActivityTimeout: 60000,

within the karma.conf. That fixed the issue.