Skip to content

approov/quickstart-ios-swift-grpc

Repository files navigation

Approov Quickstart: iOS Swift GRPC (Google Remote Procedure Call)

This quickstart is written specifically for native iOS apps that are written in Swift, making API calls using GRPC-Swift that you wish to protect with Approov. If this is not your situation, then please check if there is a more relevant quickstart guide available.

This page provides all the steps for integrating Approov into your app. Additionally, a step-by-step tutorial guide using our Shapes App Example is also available.

To follow this guide you should have received an onboarding email for a trial or paid Approov account.

Note that the minimum requirement is iOS 12. You cannot use Approov in apps that support iOS versions older than this.

ADDING APPROOV SERVICE DEPENDENCY

The Approov integration is available via the Swift Package Manager. This allows inclusion into the project by simply adding a dependency on the ApproovGRPC package in Xcode through the File -> Add Packages... menu item or in the project's Package Dependency section. In the search box of the add packages dialog enter the url of the git repository https://github.com/approov/approov-service-ios-swift-grpc.git, then choose Exact Version and use the latest available option, which should be selected for you.

Add Package Dependency

The ApproovGRPC package is actually an open source wrapper layer that allows you to easily use Approov with GRPC-Swift. This has a further dependency to the closed source Approov SDK.

USING APPROOV SERVICE

The ApproovClientConnection class mimics the interface and functionality of the ClientConnection class provided by GRPC-Swift, but also sets up pinning for the GRPC channel created by an ApproovClientConnection.Builder.

The Approov SDK wrapper class, ApproovService, needs to be initialized before using any ApproovClientConnection object. The <enter-your-config-string-here> is a custom string that configures your Approov account access. This will have been provided in your Approov onboarding email (it will be something like #123456#K/XPlLtfcwnWkzv99Wj5VmAxo4CrU267J1KlQyoz8Qo=).

The simplest way to use the ApproovClientConnection class is to find and replace all the ClientConnection with ApproovClientConnection:

try! ApproovService.initialize("<enter-your-config-string-here>")
let builder = ApproovClientConnection.usingTLSBackedByNIOSSL(on: group!)

You can then create secure pinned GRPC channels by using the returned builder instead of the usual ClientConnection.Builder:

let channel = builder.connect(host: hostname, port: port)

Approov-enable GRPC clients by adding a ClientInterceptor factory. The ClientInterceptor factory should return an ApproovClientInterceptor for any GRPC call that requires to be protected with Approov. The ApproovClientInterceptor adds an Approov-Token header to a GRPC request and may also substitute header values when using secrets protection.

// Provide the channel to the generated client.
client = Your_YourClient(channel: channel, interceptors: ClientInterceptorFactory(hostname: hostname))

The required ClientInterceptorFactory looks similar to this template and must be implemented specifically to match the code generated by the GRPC protoc compiler for your protocol definitions (.proto files). In the example below all types starting withYour_ would have been automatically generated. Note that an ApproovInterceptor needs to be returned only for GRPCs that should be protected with an Approov token.

import ApproovGRPC
import Foundation
import GRPC

// Example client interceptor factory to show how to create the required ClientInterceptorFactory for a specific
// ClientInterceptorFactoryProtocol for use with Approov.
class ClientInterceptorFactory: Your_YourClientInterceptorFactoryProtocol {

    // hostname/domain for which to add an Approov token to protected GRPC requests
    let hostname: String

    init(hostname: String) {
        self.hostname = hostname
    }

    /// - Returns: Interceptors to use when invoking a GRPC that does not require Approov protection.
    func makeUnprotectedInterceptors() -> [ClientInterceptor<Your_UnprotectedRequest, Your_UnprotectedReply>] {
        return []
    }

    /// - Returns: Interceptors to use when invoking a GRPC that requires Approov protection.
    func makeProtectedInterceptors() -> [ClientInterceptor<Your_ProtectedRequest, Your_ProtectedReply>] {
        return [ApproovClientInterceptor<Your_ProtectedRequest, Your_ProtectedReply>(hostname: hostname)]
    }
}

ERROR MESSAGES

The ApproovService functions may throw specific errors to provide additional information:

  • permanentError might be due to a feature not being enabled through using the command line
  • rejectionError an attestation has been rejected, the ARC and rejectionReasons may contain specific device information that would help troubleshooting
  • networkingError generally can be retried since it can be a temporary network issue
  • pinningError is a certificate error
  • configurationError a configuration feature is disabled or wrongly configured (e.g. attempting to initialize with different configuration from a previous initialization)
  • initializationFailure the ApproovService failed to be initialized

CHECKING IT WORKS

Initially you won't have set which API domains to protect, so the interceptor will not add anything. It will have called Approov though and made contact with the Approov cloud service. You will see logging from Approov saying unknown URL.

Your Approov onboarding email should contain a link allowing you to access Live Metrics Graphs. After you have run your app with Approov integration you should be able to see the results in the live metrics within a minute or so. At this stage you could even release your app to get details of your app population and the attributes of the devices they are running on.

NEXT STEPS

To actually protect your APIs and/or secrets there are some further steps. Approov provides two different options for protection:

  • API PROTECTION: You should use this if you control the backend API(s) being protected and are able to modify them to ensure that a valid Approov token is being passed by the app. An Approov Token is short lived crytographically signed JWT proving the authenticity of the call.

  • SECRETS PROTECTION: This allows app secrets, including API keys for 3rd party services, to be protected so that they no longer need to be included in the released app code. These secrets are only made available to valid apps at runtime.

Note that it is possible to use both approaches side-by-side in the same app.

See REFERENCE for a complete list of all of the ApproovService methods.

About

Quickstart for integrating Approov with iOS apps in Swift that make API requests you wish to protect using GRPC.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages