Skip to content

Commit

Permalink
Merge pull request #238 from SachinAkash01/api-key-auth
Browse files Browse the repository at this point in the history
Add API Key Based Authentication for Ballerina Twilio Connector
  • Loading branch information
NipunaRanasinghe authored Nov 9, 2024
2 parents 17b45c8 + 8f67ce9 commit 1bd2fe1
Show file tree
Hide file tree
Showing 25 changed files with 195 additions and 54 deletions.
6 changes: 3 additions & 3 deletions ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
distribution = "2201.8.0"
distribution = "2201.10.0"
org = "ballerinax"
name = "twilio"
version = "4.0.1"
version = "5.0.0"
authors = ["Ballerina"]
repository = "https://github.com/ballerina-platform/module-ballerinax-twilio"
keywords = ["Communication/Call & SMS", "Cost/Paid"]
icon = "icon.png"
license = ["Apache-2.0"]

[build-options]
observabilityIncluded = false
observabilityIncluded = true
33 changes: 18 additions & 15 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.8.4"
distribution-version = "2201.10.0"

[[package]]
org = "ballerina"
name = "auth"
version = "2.10.0"
version = "2.12.0"
dependencies = [
{org = "ballerina", name = "crypto"},
{org = "ballerina", name = "jballerina.java"},
Expand All @@ -22,7 +22,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "cache"
version = "3.7.1"
version = "3.8.0"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "jballerina.java"},
Expand All @@ -44,7 +44,7 @@ modules = [
[[package]]
org = "ballerina"
name = "crypto"
version = "2.5.0"
version = "2.7.2"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "time"}
Expand All @@ -53,7 +53,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "file"
version = "1.9.0"
version = "1.10.0"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
Expand All @@ -64,7 +64,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "http"
version = "2.10.7"
version = "2.12.2"
dependencies = [
{org = "ballerina", name = "auth"},
{org = "ballerina", name = "cache"},
Expand Down Expand Up @@ -96,7 +96,7 @@ modules = [
[[package]]
org = "ballerina"
name = "io"
version = "1.6.0"
version = "1.6.1"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.value"}
Expand All @@ -110,10 +110,11 @@ version = "0.0.0"
[[package]]
org = "ballerina"
name = "jwt"
version = "2.10.0"
version = "2.13.0"
dependencies = [
{org = "ballerina", name = "cache"},
{org = "ballerina", name = "crypto"},
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.int"},
{org = "ballerina", name = "lang.string"},
Expand Down Expand Up @@ -207,7 +208,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "log"
version = "2.9.0"
version = "2.10.0"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
Expand All @@ -221,17 +222,18 @@ modules = [
[[package]]
org = "ballerina"
name = "mime"
version = "2.9.0"
version = "2.10.1"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.int"}
{org = "ballerina", name = "lang.int"},
{org = "ballerina", name = "log"}
]

[[package]]
org = "ballerina"
name = "oauth2"
version = "2.10.0"
version = "2.12.0"
dependencies = [
{org = "ballerina", name = "cache"},
{org = "ballerina", name = "crypto"},
Expand All @@ -244,7 +246,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "observe"
version = "1.2.2"
version = "1.3.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand Down Expand Up @@ -277,6 +279,7 @@ version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.array"},
{org = "ballerina", name = "lang.error"}
]
modules = [
Expand All @@ -286,7 +289,7 @@ modules = [
[[package]]
org = "ballerina"
name = "time"
version = "2.4.0"
version = "2.5.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand Down Expand Up @@ -317,7 +320,7 @@ modules = [
[[package]]
org = "ballerinax"
name = "twilio"
version = "4.0.1"
version = "5.0.0"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "http"},
Expand Down
36 changes: 34 additions & 2 deletions ballerina/client.bal
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public isolated client class Client {
# + serviceUrl - URL of the target service
# + return - An error if connector initialization failed
public isolated function init(ConnectionConfig config, string serviceUrl = "https://api.twilio.com") returns error? {
self.accountSid = config.auth.username;
self.generatedClient = check new (config, serviceUrl);
self.accountSid = config.auth.accountSid;
oas:ConnectionConfig oasConnectionConfig = getOasConnectionConfig(config);
self.generatedClient = check new (oasConnectionConfig, serviceUrl);
}
# Retrieves a collection of Accounts belonging to the account used to make the request
#
Expand Down Expand Up @@ -2097,3 +2098,34 @@ public isolated client class Client {
return self.generatedClient->deleteUserDefinedMessageSubscription(accountSid ?: self.accountSid, callSid, sid);
}
}

isolated function getOasConnectionConfig(ConnectionConfig config) returns oas:ConnectionConfig => {
auth: getAuthConfig(config.auth),
httpVersion: config.httpVersion,
http1Settings: config.http1Settings,
http2Settings: config.http2Settings,
timeout: config.timeout,
forwarded: config.forwarded,
poolConfig: config.poolConfig,
cache: config.cache,
circuitBreaker: config.circuitBreaker,
compression: config.compression,
retryConfig: config.retryConfig,
responseLimits: config.responseLimits,
secureSocket: config.secureSocket,
proxy: config.proxy,
validation: config.validation
};

isolated function getAuthConfig(AuthTokenConfig|ApiKeyConfig config) returns http:CredentialsConfig {
if config is AuthTokenConfig {
return {
username: config.accountSid,
password: config.authToken
};
}
return {
username: config.apiKey,
password: config.apiSecret
};
}
4 changes: 4 additions & 0 deletions ballerina/tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ accountSid="<your-twilio-account-sid>"
authToken="<your-twilio-auth-token>"
toPhoneNumber="<your-test-phone-number>"
fromPhoneNumber="<your-twilio-phone-number>"
apiKey="<your-twilio-api-key>"
apiSecret="<your-twilio-api-secret>"
```

#### Using Environment Variables
Expand All @@ -68,6 +70,8 @@ export ACCOUNT_SID="<your-twilio-account-sid>"
export AUTH_TOKEN="<your-twilio-auth-token>"
export TO_PHONE="<your-test-phone-number>"
export TWILIO_PHONE="<your-twilio-phone-number>"
export API_KEY="<your-twilio-api-key>"
export API_SECRET="<your-twilio-api-secret>"
```

Then, run the following command to run the tests:
Expand Down
4 changes: 2 additions & 2 deletions ballerina/tests/tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ CreateMessageRequest msgReq = {
function initializeClientsForTwilioServer () returns error? {
if (isTestOnLiveServer) {
log:printInfo("Running tests on actual server");
twilio = check new ({auth: {username: accountSid, password: authToken}});
twilio = check new ({auth: {accountSid: accountSid, authToken: authToken}});
} else {
log:printInfo("Running tests on mock server");
twilio = check new ({auth: {username: accountSid, password: authToken}}, serviceUrl = "http://localhost:9090/");
twilio = check new ({auth: {accountSid: accountSid, authToken: authToken}}, serviceUrl = "http://localhost:9090/");
}
}
@test:Config {
Expand Down
24 changes: 23 additions & 1 deletion ballerina/types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import ballerina/http;
@display {label: "Connection Config"}
public type ConnectionConfig record {|
# Configurations related to client authentication
http:CredentialsConfig auth;
AuthTokenConfig|ApiKeyConfig auth;
# The HTTP version understood by the client
http:HttpVersion httpVersion = http:HTTP_2_0;
# Configurations related to HTTP/1.x protocol
Expand Down Expand Up @@ -55,6 +55,28 @@ public type ConnectionConfig record {|
boolean validation = true;
|};

# Twilio Auth token Based Authentication configuration.
#
# + accountSid - Twilio account SID
# + authToken - Twilio authentication token of the account
@display{label: "Auth token based authentication config"}
public type AuthTokenConfig record {|
string accountSid;
string authToken;
|};

# Twilio API Key Based Authentication configurations.
#
# + apiKey - Twilio API key SID
# + apiSecret - Twilio API key Secret
# + accountSid - Twilio account SID
@display{label: "API Key Based authentication config"}
public type ApiKeyConfig record {|
string apiKey;
string apiSecret;
string accountSid;
|};

# Provides settings related to HTTP/1.x protocol.
public type ClientHttp1Settings record {|
# Specifies whether to reuse a connection for multiple requests
Expand Down
2 changes: 1 addition & 1 deletion build-config/resources/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
distribution = "2201.8.0"
distribution = "2201.10.0"
org = "ballerinax"
name = "twilio"
version = "@toml.version@"
Expand Down
4 changes: 2 additions & 2 deletions examples/accounts/create-sub-account/main.bal
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ configurable string authToken = os:getEnv("AUTH_TOKEN");
// Twilio configurations
twilio:ConnectionConfig twilioConfig = {
auth: {
username: accountSid,
password: authToken
accountSid,
authToken
}
};

Expand Down
4 changes: 2 additions & 2 deletions examples/accounts/fetch-account/main.bal
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ configurable string authToken = os:getEnv("AUTH_TOKEN");
// Twilio configurations
twilio:ConnectionConfig twilioConfig = {
auth: {
username: accountSid,
password: authToken
accountSid,
authToken
}
};

Expand Down
4 changes: 2 additions & 2 deletions examples/accounts/fetch-balance/main.bal
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ configurable string authToken = os:getEnv("AUTH_TOKEN");
// Twilio configurations
twilio:ConnectionConfig twilioConfig = {
auth: {
username: accountSid,
password: authToken
accountSid,
authToken
}
};

Expand Down
4 changes: 2 additions & 2 deletions examples/accounts/list-accounts/main.bal
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ configurable string authToken = os:getEnv("AUTH_TOKEN");
// Twilio configurations
twilio:ConnectionConfig twilioConfig = {
auth: {
username: accountSid,
password: authToken
accountSid,
authToken
}
};

Expand Down
4 changes: 2 additions & 2 deletions examples/accounts/update-account/main.bal
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ configurable string authToken = os:getEnv("AUTH_TOKEN");
// Twilio configurations
twilio:ConnectionConfig twilioConfig = {
auth: {
username: accountSid,
password: authToken
accountSid,
authToken
}
};

Expand Down
26 changes: 26 additions & 0 deletions examples/authentication/api-key-auth/API key authentication.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# API Key Based Authentication in Ballerina Twilio Connector

This example demonstrates how to authenticate your requests to Twilio Account using API Key based authentication in Ballerina Twilio Connector.

## Prerequisites

### 1. Set up
Refer to the setup guide in [ReadMe](../../../README.md) for necessary credentials.

### 2. Configuration

Configure Twilio API credentials in `Config.toml` in the example directory:

```toml
API_KEY="<API Key>"
API_SECRET="<API Secret>"
ACCOUNT_SID="<Account Sid>"
```

## Run the Example

Execute the following command to run the example:

```bash
bal run
```
8 changes: 8 additions & 0 deletions examples/authentication/api-key-auth/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
org = "twilio_samples"
name = "api_key_auth"
version = "0.1.0"
distribution = "2201.10.1"

[build-options]
observabilityIncluded = true
Loading

0 comments on commit 1bd2fe1

Please sign in to comment.