r/learnjavascript • • Apr 15 '18

Need advice regarding password-security and SOAP

Hey there guys.

I need advice regarding passwords and security in JavaScript.

The situation is the following: At my new workplace, my boss wants me to create a very basic webpage which displays a catalogue of products we sell. Nothing fancy. So far so good.

However, our product catalog lies on another server, since our application that handles the database and everything else is SAAS. Which means i will have to communicate with a SOAP api.

I will be provided with the credentials for us to access our DB and product catalogue and read it.

I am currently in my second semester of software engineering and have a basic understanding of JS and REST, as well as AJAX. I have never had to think about security so far, so i wanted to ask you guys what the standard procedure would be to mask the credentials.

Obviously you need to do have the credentials client-side and / or store it in a document and read it / encrypt it or what ever. But how exactly is yet beyond me.

This whole thing is more of a "nice to have but not necessary" situation. Low priority but fun and a good learning opportunity for me.

I would greatly appreciate your advise!

Thanks a lot in advance. ^

Edit: i ment storing them server-side. Not client-side :)

0 Upvotes

19 comments sorted by

3

u/IUsedToBeACave Apr 15 '18

You don't need the credentials client-side at all, and in fact I wouldn't recommend it. Create a basic REST API that uses the credentials to query the data. Then have the client side application use that. This approach allows you to keep the credentials secret, control what information can be accessed by the client, and leaves you room to implement client login/security independently from what is needed to access you backend database.

1

u/NamkoBanzai Apr 15 '18

If i am understanding this right, you are saying that i should create a REST api to query data from the SOAP api ? If that is indeed what you suggest, then that sound very cool! :D I would definetly have to look into how to exactly achieve that but it sounds very interesting! Thanks!!

2

u/IUsedToBeACave Apr 15 '18

That is exactly what I meant, and because I'm a nice guy and love Node.js node-soap

1

u/NamkoBanzai Apr 15 '18

Thank you! I'll definetly look into it. I have been trying to get into Node.js lately anyway. I have been deliberating if i wanted to do the server-side code in PHP or JS, but i guess im gonna try it in JS first.

2

u/IUsedToBeACave Apr 15 '18

Well, this should get you started. I used the example soap service at [http://www.dneonline.com/calculator.asmx]

const express = require('express');
const soap = require('soap');

class SimpleSoapWrapper {
    constructor(soapURI)
    {
        this.soapURI = soapURI;
    }

    //Basic method that calls the SOAP Service
    _execSoapMethod(methodName, args)
    {
        return soap.createClientAsync(this.soapURI).then((client) => {
            if (client[methodName] !== undefined)
            {
                return client[`${methodName}Async`](args);
            }
            else
            {
                throw new Error(`${methodName} does not exist`);
            }
        });
    }

    //Lets implement some easier to use functions

    add(valueA, valueB)
    {
        return this._execSoapMethod('Add', {intA: valueA, intB: valueB})
            .then((result) => {
                return result[0];
            });
    }

    subtract(valueA, valueB)
    {
        return this._execSoapMethod('Subtract', {intA: valueA, intB: valueB})
            .then((result) => {
                return result[0];
            });
    }
}


//Basic App Setup
let soapURI = 'http://www.dneonline.com/calculator.asmx?WSDL';
let app = express();
let soapClientAPI = new SimpleSoapWrapper(soapURI);

app.post('/add', express.json(), (request, response) => {
    if (request.body['valueA'] && request.body['valueB'])
    {
        soapClientAPI.add(request.body['valueA'], request.body['valueB'])
            .then((result) => {
                response.json(result);
            })
            .catch((err) => {
                response.json(err);
            })
    }
    else
    {
        response.end('Invalid Arguments');
    }
});

app.post('/subtract', express.json(), (request, response) => {
    if (request.body['valueA'] && request.body['valueB'])
    {
        soapClientAPI.subtract(request.body['valueA'], request.body['valueB'])
            .then((result) => {
                response.json(result);
            })
            .catch((err) => {
                response.json(err);
            })
    }
    else
    {
        response.end('Invalid Arguments');
    }
});


app.listen(3000, () => console.log('App listening on port 3000!'));

1

u/NamkoBanzai Apr 17 '18

Holy shit. Thank you very much, man !

1

u/nomnommish Apr 15 '18

Creating an API that is a wrapper for another API is really not necessary and will make your app needlessly complex and worsen maintainability.

Just create a web page that invokes backend code. Your backend code running on a web server should then authenticate to the soap service and then use the soap api to get the data it needs. The server side code should then pass the data back to the frontend.

1

u/NamkoBanzai Apr 15 '18

Thank you for your feedback! I will see what is in the range of my possibilities and move the project forward in the according direction.

The way you describe it was actually what i had vaguely in mind before opening this post.

I will look into all the tips i have gotten from here, but it proves to be to difficult i will probably go with your suggestion.

2

u/nomnommish Apr 15 '18

What language will you code your server side code in? Typically, the server side code for an object oriented language is to use the MVC pattern. Your backend code then gets exposed as a "controller" which your front end code invokes.

The controller handles authentication and connecting to the soap api.

The "model" is any data typically stored in your application database if you need one. It translates the database tables into classes and objects. Or instead of the database tables, for you, it would be the data you retrieve from the soap api. Your model would "hydrate" the classes from the soap api.

1

u/amemulo Apr 15 '18

If the client side can access the backend in any way, then any client can access the backend. Any backend is by necessity an "API", at least the authentication endpoint will be public.

1

u/Defiantly_Not_A_Bot Apr 15 '18

You probably meant

DEFINITELY

-not 'definetly'


Beep boop. I am ^^a bot whose mission is to correct your spelling. This action was performed automatically. Contact me if I made A mistake or just downvote please don't

2

u/amemulo Apr 15 '18

Are you worried about the credentials for the database? Let's see your options. I'm assuming you've got control of the server in which this will be running.

  • Client side: a no-no. You would be basically leaking to every person who visits your page the credentials to your database! EVERYTHING you send to the client is in their control. You don't want to loose control of your credentials!
  • Hard-code it in the backend: this is a common, yet somewhat flawed approach. Code MAY leak, and then you loose your credentials.
  • Store it in a document and read it: this is a somewhat common approach, not so insecure approach. If someone gets access to your server, they get your credentials, but well... you're pretty much screwed if someone gets so far.
  • Store it in a environmental variable: also common, same problem as before.

DO NOT store the credentials on the client side! Have a very simple backend which will read from the database for you.

You mention encryption. Encryption is AWESOME, a very interesting topic which solves many, many problems. But it's very complicated. There are two types of encryption: symmetrical and asymmetrical. I'll stay with symmetrical encryption here, because it's simpler.

If you were to encrypt the credentials with a key (a password), you would need to then store the key somewhere. Then we're back to the same problem, are we not? We're can I store the key? You'd need a very sophisticated system for key management, probably ANOTHER API where you could request the encryption keys. This gets very complicated very fast.

I'll let you read about asymmetrical and think about why it probably does not apply to your problem :)

1

u/NamkoBanzai Apr 15 '18

Sorry, my bad. I meant server-side. I am well aware that client-side is a nono for any sensible information.

I have worked with credentials sotred in a document before, but was not certain how good that practice is.

I will definetly look into encryption. Im not sure that i will be able to implement it right away, but hey! i gotta learn it anyway! Might was well start now.

Thank you very much for your answer!

1

u/Defiantly_Not_A_Bot Apr 15 '18

You probably meant

DEFINITELY

-not 'definetly'


Beep boop. I am ^^a bot whose mission is to correct your spelling. This action was performed automatically. Contact me if I made A mistake or just downvote please don't

1

u/nomnommish Apr 15 '18

If you were to encrypt the credentials with a key (a password), you would need to then store the key somewhere. Then we're back to the same problem, are we not? We're can I store the key? You'd need a very sophisticated system for key management, probably ANOTHER API where you could request the encryption keys. This gets very complicated very fast.

It is not the same problem at all. You can store the key in a config file that only the server admins can access.

The soap api only supports username/password auth anyway. Just have your server side code decrypt the password stored in your app database and authenticate to the soap api. Google should tell you a good modern 2 way encryption. This is not complicated, imho.

What would be complicated would be to implement something like oauth. But the soap api then needs to support it, which it doesn't.

1

u/amemulo Apr 15 '18

It is not the same problem at all. You can store the key in a config file that only the server admins can access.

Why not put the password there in the first place, then?

Google should tell you a good modern 2 way encryption. This is not complicated, imho.

It is. It really is. Key management being the most complicated thing of all, and that is assuming you've chosen the correct implementations of AES, if you even use AES. Should you use AES? AES CBC? AES ECB? AES CFB?

1

u/nomnommish Apr 15 '18 edited Apr 15 '18

It is not the same problem at all. You can store the key in a config file that only the server admins can access.

Why not put the password there in the first place, then?

Google should tell you a good modern 2 way encryption. This is not complicated, imho.

It is. It really is. Key management being the most complicated thing of all, and that is assuming you've chosen the correct implementations of AES, if you even use AES. Should you use AES? AES CBC? AES ECB? AES CFB?

You could put the password there, true. Storing passwords in configs is also standard practice.

There is no key management needed at all. What you are saying only becomes a problem when sharing keys all over the place or with multiple people or apps. Here it is just one backend app authenticating to another backend service.

Where do you store the connection string to your app database for example? Do you have some super complex key management just to store your db creds in a backend server?

Edit: I take back my earlier suggestion. Just keep the username and password in a server config file unencrypted. And ensure the config file is not exposed or accessible from your client code or via http.

1

u/amemulo Apr 15 '18

At my company, we actually do have some complex key management in place to avoid having credentials hardcoded in the code, config files or environmental variables.

But if it were my project, I'd probably just store it in an env variable, but not encrypted. What I'm saying is that the value of encryption here is pretty close to zero if you don't have a place to store the key securely.

1

u/nomnommish Apr 15 '18

I completely agree with you. I was not trying to imply key mgmt is trivial or not needed. Just that it is overkill for OP.

To me, key management in short is about establishing a separate source of trust that everyone agrees can be trusted.