Implementing secure login with Swift and a PHP API

  • Thread starter ergospherical
  • Start date
  • Tags
    Php
In summary, implementing secure login with Swift and a PHP API involves using SSL encryption, verifying user credentials with a secure database, and implementing a token-based authentication system. This ensures that sensitive user information is protected during the login process, and that only authorized users have access to the app's features. By following best practices and properly securing the API endpoints, this method can provide a robust and reliable solution for secure login in mobile apps.
  • #1
ergospherical
976
1,278
I'm hosting a database containing usernames and password hashes, and have written a little PHP API which accepts HTTP post requests (sent from an iOS application via Alamofire for Swift) containing two parameters, username & password, checks against the database & returns some JSON data containing a truth value signifying whether to validate the login, plus some accompanying information.

This method worked, but I cannot use it because HTTP post requests are not encrypted and the approach is pretty much totally unsecure. I am trying to figure out how best to reprogram the login - or use an existing library? In either case I would be interested to hear if you have experience with this sort of thing. Thanks!
 
Technology news on Phys.org
  • #3
jedishrfu said:
Try using https instead.
...but still use a library. I recommend either Laravel if you want a completely pre-packed solution, or roll your own using the Symfony Request object (and then you will also want to add routing using e.g. FastRoute).
 
  • Informative
Likes ergospherical
  • #4
jedishrfu said:
Try using https instead.
How would I do this with Alamofire? The relevant portion of my code is currently :
Swift:
 @IBAction func loginPressed(_ sender: Any) {
        let username = usernameField.text!
        let password = passwordField.text!
      
        let parameters: Parameters=[
            "username":username,
            "password":password
        ]
    
        Alamofire.request(URL_USER_REGISTER, method: .post, parameters: parameters).responseJSON
            {
                response in
                if let result = response.result.value {
                    let jsonData = result as! NSDictionary
                    if(!(jsonData.value(forKey: "error") as! Bool)){
                        let validated = jsonData.value(forKey: "validated") as! Int
                        if(validated==1){
                            //Irrelevant stuff omitted for brevity
                        }
                    }else{
                        //error message in case of invalid credential
                    }
                }
        }
    }
 
  • #5
URL_USER_REGISTER needs to start with https:// instead of http://
 
  • Like
Likes ergospherical
  • #6
PHP's $_POST global is populated identically whether the request is HTTP or HTTPS.
 
  • Like
Likes ergospherical
  • #7
Oh right, is it that simple of a change? And there's no glaring security vulnerabilities of using this approach to user authentication? Thanks for the speedy replies by the way.
 
  • #8
Well the passwords should be salted and hashed appropriately and access to that database restricted (bearing in mind any automated backups). If you can't manage the security properly then you are better off using OAUTH 2 authentication via e.g. a Google login.
 
  • Like
Likes Filip Larsen, jedishrfu and ergospherical
  • #9
But 99% of PHP-driven web sites (including I wouldn't mind betting this one) just use PHP's default 10 rounds of bcrypt stored in a SQL table backed up by a web host along with the rest of the database.
 
  • Like
Likes jedishrfu and ergospherical
  • #10
pbuk said:
Well the passwords should be salted and hashed appropriately and access to that database restricted (bearing in mind any automated backups). If you can't manage the security properly then you are better off using OAUTH 2 authentication via e.g. a Google login.
The salting and hashing I'm pretty confident about (well, I solicited some help from one of my friend's siblings who dabbles in cybersecurity :oldbiggrin:). I just need to be sure that I'm not doing anything stupid transferring data between the app and the API, but I'm reassured now that it's probably fine.
 
  • Like
Likes jedishrfu
  • #11
The transfer is the one thing you don't have to worry about - https is implemented very securely by the client and the server. The other thing you need to watch is logging: web servers don't store plain text logs of request bodies (i.e. $_POST variables) by default (they DO store plain text logs of query strings i.e. $_GET variables), but be careful about implementing e.g. crash dumps of unsanitised $_POST variables.
 
  • Informative
Likes ergospherical
  • #12
Oh and you will want to make sure that the incoming request IS actually HTTPS, either by enforcing a 301 redirect in an .htaccess file (assuming Apache) or by checking $_SERVER (again preferably indirectly e.g. with a Symfony $request->isSecure()). Or both.
 
  • Informative
Likes ergospherical
  • #14
Make sure you properly quote any user input so as to prevent an injection attack. PHP may do that already But it’s good to do your due diligence and check it out.

The recent log4j crisis is predicated on the possibility that someone tries an injection attack and the web app logs it starting the disastrous chain of events.
 
  • Like
Likes ergospherical

Related to Implementing secure login with Swift and a PHP API

1. What is the purpose of implementing secure login with Swift and a PHP API?

The purpose of implementing secure login with Swift and a PHP API is to ensure that only authorized users are able to access the sensitive data and features of an application. This helps to protect user privacy and prevent unauthorized access or malicious attacks.

2. What are the main components involved in implementing secure login with Swift and a PHP API?

The main components involved in implementing secure login with Swift and a PHP API include a user interface in Swift, a server-side API written in PHP, and a database to store user credentials and other sensitive information.

3. How does secure login with Swift and a PHP API work?

The process of secure login with Swift and a PHP API involves the user entering their login credentials into the app, which are then sent to the server-side API for verification. The API checks the credentials against the database and returns a response to the app, either granting or denying access based on the authentication result.

4. What security measures should be taken when implementing secure login with Swift and a PHP API?

Some important security measures to consider when implementing secure login with Swift and a PHP API include using encryption to protect sensitive data, implementing secure password storage practices, and implementing measures to prevent brute force attacks and other malicious activities.

5. Are there any potential vulnerabilities to be aware of when implementing secure login with Swift and a PHP API?

Yes, there are potential vulnerabilities that should be considered when implementing secure login with Swift and a PHP API. These may include SQL injection attacks, cross-site scripting (XSS) attacks, and other common web security vulnerabilities that could compromise the security of the API and user data. It is important to thoroughly test and secure the API to prevent these vulnerabilities.

Similar threads

  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Feedback and Announcements
Replies
0
Views
95K
Back
Top