nativescript-oauth2 icon indicating copy to clipboard operation
nativescript-oauth2 copied to clipboard

Support Keycloak

Open kstan79 opened this issue 7 years ago • 12 comments

Hi,

Thanks of this plugin and it reduce my work. I'd modify providers/provider.js to support keycloak, tested it work in ios only. Android it show blank page instead of login form (Seems SSL issue), after by pass SSL it still facing another issue which is webview no callback success status (another error which is session not exists). Need your advise how to make it work in android.

Below is the additional code of provider.js:

//add keycloak into provider.js
var TnsOaProviderKeycloak = (function () {
    function TnsOaProviderKeycloak(options,keycloakbaseurl,realm) {
        this.openIdSupport = "oid-none";
        this.providerType = "keycloak";
        this.authority = keycloakbaseurl;
        this.tokenEndpointBase = keycloakbaseurl;
        this.authorizeEndpoint = '/realms/'+realm+'/protocol/openid-connect/auth';
        this.tokenEndpoint = '/realms/'+realm+'/protocol/openid-connect/token';
        this.cookieDomains = [realm];
        this.options = options;
    }
    TnsOaProviderKeycloak.prototype.parseTokenResult = function (jsonData) {
        return jsonData;
    };
    return TnsOaProviderKeycloak;
}());
exports.TnsOaProviderKeycloak = TnsOaProviderKeycloak;

I'm not really understand how nativescript plugin system (how .ts convert to .js) work, and the plugin seems no distribute provider.ts (in fact, our project not use typescript). So I submit above code hope you can you merge into your project and benefit others keycloak user.

for usage, auth-service.js:

function configureOAuthProviderKeycloak() {
  var serverurl='https://<keycloakserver>/auth';
  var realm='keycloakrealm';
    var keycloakProviderOptions = {        
        openIdSupport: "oid-none",
        clientId: "keycloakclientid",
        clientSecret: "clientsecret",
        redirectUri: "redirecturl",        
        scopes: ["email"]
    };
    var keycloakProvider = new providers_1.TnsOaProviderKeycloak(keycloakProviderOptions,serverurl,realm);
    return keycloakProvider;
}

at landingpage, I use below:

var auth_service_1 = require("~/auth-service");
auth_service_1.configureOAuthProviders();
auth_service_1.tnsOauthLogin("keycloak");

Thanks Ks

kstan79 avatar Nov 29 '18 01:11 kstan79

screenshot 2018-11-30 at 8 30 42 pm You can refer above image, seems it server side received multiple access in same second. below is nativescript event, it shown first attemp success, and immediately show 2nd attemp error. screenshot 2018-11-30 at 8 33 03 pm

hope you have some clue.

kstan79 avatar Nov 30 '18 12:11 kstan79

Thanks for the suggestion. Is there an open environment where I can test the keycloak auth connection? As far as TypeScript - using the plugin does not require TypeScript, you can just use JavaScript if you wish. TypeScript is there only if you need it.

alexziskind1 avatar Dec 01 '18 20:12 alexziskind1

Hi, you may use this, I'd make this allow registration. If you encounter any issue please dont hesitate to let me know.

** By the way, oid-none and oid-full I'm notice has different behaviour but I have no ideal which one is better for us.

function configureOAuthProviderKeycloak() {
  var serverurl='https://login.simbiz.cloud/auth';
  var realm='simbiz.cloud';
    var keycloakProviderOptions = {        
        openIdSupport: "oid-none", 
        clientId: "mytesting",
        clientSecret: "7139caee-87fb-4f1f-a014-76eb4fbe61f6",
        redirectUri: "http://localhost/a.html",
        scopes: ["email","uid","name"]
    };
    var keycloakProvider = new providers_1.TnsOaProviderKeycloak(keycloakProviderOptions,serverurl,realm);
    return keycloakProvider;
}

kstan79 avatar Dec 03 '18 02:12 kstan79

hi, after some try seems above amendment is work (previously seems server side ssl missing origin ca-bundle) so android blocked. I believe you can make this as standard

//add keycloak into provider.js
var TnsOaProviderKeycloak = (function () {
    function TnsOaProviderKeycloak(options,keycloakbaseurl,realm) {
        this.openIdSupport = "oid-none";
        this.providerType = "keycloak";
        this.authority = keycloakbaseurl;
        this.tokenEndpointBase = keycloakbaseurl;
        this.authorizeEndpoint = '/realms/'+realm+'/protocol/openid-connect/auth';
        this.tokenEndpoint = '/realms/'+realm+'/protocol/openid-connect/token';
        this.cookieDomains = [realm];
        this.options = options;
    }
    TnsOaProviderKeycloak.prototype.parseTokenResult = function (jsonData) {
        return jsonData;
    };
    return TnsOaProviderKeycloak;
}());
exports.TnsOaProviderKeycloak = TnsOaProviderKeycloak;

kstan79 avatar Dec 07 '18 07:12 kstan79

Hi, I'm reopen this issue just for follow up hope can make keycloak officially support

kstan79 avatar Apr 18 '19 07:04 kstan79

@kstan79 could you create a PR for this? would be great to have keycloak supported by this package. how do you add this manually to the code?

chongma avatar Jan 09 '20 10:01 chongma

I add above sample into project/node_modules/nativescript-oauth2/providers/providers.js

then change auth-service.js and add following sample


function configureOAuthProviderKeycloak() {
  let customizeLoginServer = filestorage.readCustomizeLoginServer();

  let server ='https://yourkeycloakserver/auth';
  let realm =  'yourrealm';
  let keycloakProviderOptions = {
        openIdSupport: 'oid-none',  
        clientId:  'yourclientid',
        clientSecret: 'your secret',
        redirectUri: 'https://yourvalidredirecturl',
        scopes: ['email','uid','name','links']
    };

    let keycloakProvider = new providers.TnsOaProviderKeycloak(keycloakProviderOptions, server, realm);
    return keycloakProvider;
}

kstan79 avatar Jan 09 '20 12:01 kstan79

@kstan79 Thanks for your help. i added the keycloak authprovider in my project and it works fine. i didn't add it into project/node_modules/nativescript-oauth2/providers/providers.js in case the node_modules directory is updated or deleted. it would be nice if the provider was added to this project though and then i wouldn't have to define it

chongma avatar Jan 10 '20 14:01 chongma

Would be great to add Keycloak support.

SmailHammour avatar Mar 30 '20 13:03 SmailHammour

Actually I'm usign keycloak just by creating a new provider. I'm using "oid-none". On development machine, when launching the application in the emulator, you need to point to this host: 10.0.2.2, it will redirect the call to the host machine 'localhost'. It's stated in android documentation. Hope it helps!

trane77 avatar Apr 03 '20 17:04 trane77

Thank you all for the comments and @kstan79 for the code, it was the missing piece for me 👍

Following the guide to create a custom provider and @funder7 's comment you no longer need to modify the providers.js file inside of the nativescript-oauth2 package, you can create your own keycloak provider and use it.

Please see Keycloak provider for nativescript-oauth2 inside a svelte native application.

It seems to me the issue can be kind of closed now 🤔

davidpodhola avatar Oct 21 '20 06:10 davidpodhola

Glad you solved it @davidpodhola! Probably it's better to close it after adding some info in the documentation, it can help other users to use keycloak without digging into the repository issues :-)

trane77 avatar Oct 22 '20 16:10 trane77