Skip navigation
A page of code that includes the userAgent string Duo API Swift.
duo labs

Announcing DuoAPISwift: A Duo API Client for Swift

TL;DR: Today, we released a new API client for the Swift Programming language on GitHub. We’d love for you to try it out!

At Duo Security, we believe in democratizing security. We do this by delivering easy-to-use security technologies to our customers and their end users. As part of this, we also take great care to make it easy for administrators to interact with our APIs. So when it came time to write new tooling in Swift at Duo, we decided to take a little extra time and write an API client that not only we could use, but could also be released to the public.

Motivation

Swift is a powerful, general-purpose programming language that has been growing in popularity since its release in 2014. Many iOS, macOS, watchOS, and tvOS applications are written in it today, but it’s also been gaining popularity as a server-side language. Apple even gave a talk about this use case at its Worldwide Developers Conference (WWDC) in 2016. In light of this, it seemed appropriate for us to make Duo’s powerful Auth API available to Swift developers, whether they’re making apps for Apple platforms or writing web services in Swift on Linux.

Use Cases

The Duo Authentication API allows customers to add strong two-factor authentication to their website or application using a low-level, RESTful API. Our customers are free to interact with our API using any programming language they choose, but we try to simplify things by offering client libraries in a variety of programming languages.

Our client libraries abstract things like authentication, so all you have to worry about is creating the Auth API application in the Duo Admin Panel and plugging in the automatically generated integration key, secret key and API URL.

In addition to today’s release of the Swift client library, we also currently have client libraries available in the following programming languages:

The Auth API itself can be used to check if our service is up and running, enroll a device in Duo, or send an authentication request to a user, (e.g., a Duo Push notification). For detailed instructions on installing the Duo Swift Library, see our DuoAPISwift GitHub repository. It’s also worth noting that for simpler web-based integrations we offer the Duo Web SDK.

Example Usage

In order to get up and running with the DuoAPISwift library, you’ll need to create an Auth API application in the Duo Admin Panel. Once this is done, you can enter the automatically generated values in a Swift project or script to see how it works!

Here’s an example of how to send a Duo Push authentication request to a user.

    let auth = Auth(
        ikey: "<IKEY>",
        skey: "<SKEY (Keep this secret!)>",
        host: "api-xxxxxxxx.duosecurity.com")


    auth.auth("push",
              username: "<USERNAME>",
              device: "auto",
              completion: { response in
        var allowed = false
        if let r = response["response"],
               result = r?["result"] as? String {
            if result == "allow" {
                allowed = true
            }
        }
        if allowed {
            print("Success. Logging you in...")
        } else {
            print("Access denied.")
        }
    })

That’s it! Just like that, you’re able to send a Duo Push notification to a user and grant them access if they tapped approve.

Summary

We hope DuoAPISwift will make our powerful APIs accessible to even more developers and software projects. So go ahead, take it for a spin! If you have any questions, feel free to reach us at labs@duosecurity.com. You can also file bug reports, open pull requests, or share your favorite GIF with us on the DuoAPISwift GitHub repository! We can’t wait to see what you build next!