Skip to content

Commit

Permalink
Merge pull request #76 from ldclakmal/dev-design
Browse files Browse the repository at this point in the history
Update ballerina/jwt module for Swan Lake Design
  • Loading branch information
ldclakmal authored Jan 5, 2021
2 parents ea3c559 + 5f08776 commit 9c49984
Show file tree
Hide file tree
Showing 20 changed files with 483 additions and 614 deletions.
35 changes: 4 additions & 31 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@ plugins {
}

ext.ballerinaLangVersion = project.ballerinaLangVersion
ext.stdlibAuthVersion = project.stdlibAuthVersion
ext.stdlibCacheVersion = project.stdlibCacheVersion
ext.stdlibCryptoVersion = project.stdlibCryptoVersion
ext.stdlibEncodingVersion = project.stdlibEncodingVersion
ext.stdlibLogVersion = project.stdlibLogVersion
ext.stdlibStringUtilsVersion = project.stdlibStringUtilsVersion
ext.stdlibSystemVersion = project.stdlibSystemVersion
ext.stdlibTimeVersion = project.stdlibTimeVersion
ext.stdlibUuidVersion = project.stdlibUuidVersion
// Transitive dependencies
ext.stdlibConfigVersion = project.stdlibConfigVersion
ext.stdlibIoVersion = project.stdlibIoVersion
ext.stdlibRuntimeVersion = project.stdlibRuntimeVersion
ext.stdlibTaskVersion = project.stdlibTaskVersion

Expand Down Expand Up @@ -63,13 +60,6 @@ allprojects {
password System.getenv("packagePAT")
}
}
maven {
url = 'https://maven.pkg.github.com/ballerina-platform/module-ballerina-auth'
credentials {
username System.getenv("packageUser")
password System.getenv("packagePAT")
}
}
maven {
url = 'https://maven.pkg.github.com/ballerina-platform/module-ballerina-cache'
credentials {
Expand Down Expand Up @@ -105,35 +95,21 @@ allprojects {
password System.getenv("packagePAT")
}
}
maven {
url = 'https://maven.pkg.github.com/ballerina-platform/module-ballerina-system'
credentials {
username System.getenv("packageUser")
password System.getenv("packagePAT")
}
}
maven {
url = 'https://maven.pkg.github.com/ballerina-platform/module-ballerina-time'
credentials {
username System.getenv("packageUser")
password System.getenv("packagePAT")
}
}
// Transitive dependencies
maven {
url = 'https://maven.pkg.github.com/ballerina-platform/module-ballerina-config'
credentials {
username System.getenv("packageUser")
password System.getenv("packagePAT")
}
}
maven {
url = 'https://maven.pkg.github.com/ballerina-platform/module-ballerina-io'
url = 'https://maven.pkg.github.com/ballerina-platform/module-ballerina-uuid'
credentials {
username System.getenv("packageUser")
password System.getenv("packagePAT")
}
}
// Transitive dependencies
maven {
url = 'https://maven.pkg.github.com/ballerina-platform/module-ballerina-runtime'
credentials {
Expand All @@ -159,17 +135,14 @@ subprojects {
}
dependencies {
/* Standard libraries */
ballerinaStdLibs "org.ballerinalang:auth-ballerina:${stdlibAuthVersion}"
ballerinaStdLibs "org.ballerinalang:cache-ballerina:${stdlibCacheVersion}"
ballerinaStdLibs "org.ballerinalang:crypto-ballerina:${stdlibCryptoVersion}"
ballerinaStdLibs "org.ballerinalang:encoding-ballerina:${stdlibEncodingVersion}"
ballerinaStdLibs "org.ballerinalang:log-ballerina:${stdlibLogVersion}"
ballerinaStdLibs "org.ballerinalang:stringutils-ballerina:${stdlibStringUtilsVersion}"
ballerinaStdLibs "org.ballerinalang:system-ballerina:${stdlibSystemVersion}"
ballerinaStdLibs "org.ballerinalang:time-ballerina:${stdlibTimeVersion}"
ballerinaStdLibs "org.ballerinalang:uuid-ballerina:${stdlibUuidVersion}"
// Transitive dependencies
ballerinaStdLibs "org.ballerinalang:config-ballerina:${stdlibConfigVersion}"
ballerinaStdLibs "org.ballerinalang:io-ballerina:${stdlibIoVersion}"
ballerinaStdLibs "org.ballerinalang:runtime-ballerina:${stdlibRuntimeVersion}"
ballerinaStdLibs "org.ballerinalang:task-ballerina:${stdlibTaskVersion}"
}
Expand Down
5 changes: 1 addition & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ org.gradle.caching=true
group=org.ballerinalang
version=1.0.7-SNAPSHOT
ballerinaLangVersion=2.0.0-Preview9-SNAPSHOT
stdlibAuthVersion=1.0.5-SNAPSHOT
stdlibCacheVersion=2.0.5-SNAPSHOT
stdlibCryptoVersion=1.0.5-SNAPSHOT
stdlibEncodingVersion=1.0.7-SNAPSHOT
stdlibLogVersion=1.0.5-SNAPSHOT
stdlibStringUtilsVersion=0.5.5-SNAPSHOT
stdlibSystemVersion=0.6.5-SNAPSHOT
stdlibTimeVersion=1.0.6-SNAPSHOT
stdlibUuidVersion=0.9.1-SNAPSHOT
# Transitive dependencies
stdlibConfigVersion=1.0.5-SNAPSHOT
stdlibIoVersion=0.5.5-SNAPSHOT
stdlibRuntimeVersion=0.5.5-SNAPSHOT
stdlibTaskVersion=1.1.5-SNAPSHOT
18 changes: 0 additions & 18 deletions issue_template.md

This file was deleted.

13 changes: 4 additions & 9 deletions jwt-ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ org = "ballerina"
name = "jwt"
version = "@toml.version@"

[[dependency]]
org = "ballerina"
name = "auth"
version = "@stdlib.auth.version@"

[[dependency]]
org = "ballerina"
name = "cache"
Expand Down Expand Up @@ -35,13 +30,13 @@ version = "@stdlib.stringutils.version@"

[[dependency]]
org = "ballerina"
name = "system"
version = "@stdlib.system.version@"
name = "time"
version = "@stdlib.time.version@"

[[dependency]]
org = "ballerina"
name = "time"
version = "@stdlib.time.version@"
name = "uuid"
version = "@stdlib.uuid.version@"

[[platform.java11.dependency]]
path = "../jwt-native/build/libs/jwt-native-@project.version@.jar"
Expand Down
5 changes: 0 additions & 5 deletions jwt-ballerina/ballerina.conf

This file was deleted.

6 changes: 2 additions & 4 deletions jwt-ballerina/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,23 @@ def originalConfig = ballerinaConfigFile.text

task updateTomlVersions {
doLast {
def stdlibDependentAuthVersion = project.stdlibAuthVersion.split("-")[0]
def stdlibDependentCacheVersion = project.stdlibCacheVersion.split("-")[0]
def stdlibDependentCryptoVersion = project.stdlibCryptoVersion.split("-")[0]
def stdlibDependentEncodingVersion = project.stdlibEncodingVersion.split("-")[0]
def stdlibDependentLogVersion = project.stdlibLogVersion.split("-")[0]
def stdlibDependentStringUtilsVersion = project.stdlibStringUtilsVersion.split("-")[0]
def stdlibDependentSystemVersion = project.stdlibSystemVersion.split("-")[0]
def stdlibDependentTimeVersion = project.stdlibTimeVersion.split("-")[0]
def stdlibDependentUuidVersion = project.stdlibUuidVersion.split("-")[0]

def newConfig = ballerinaConfigFile.text.replace("@project.version@", project.version)
newConfig = newConfig.replace("@toml.version@", tomlVersion)
newConfig = newConfig.replace("@stdlib.auth.version@", stdlibDependentAuthVersion)
newConfig = newConfig.replace("@stdlib.cache.version@", stdlibDependentCacheVersion)
newConfig = newConfig.replace("@stdlib.crypto.version@", stdlibDependentCryptoVersion)
newConfig = newConfig.replace("@stdlib.encoding.version@", stdlibDependentEncodingVersion)
newConfig = newConfig.replace("@stdlib.log.version@", stdlibDependentLogVersion)
newConfig = newConfig.replace("@stdlib.stringutils.version@", stdlibDependentStringUtilsVersion)
newConfig = newConfig.replace("@stdlib.system.version@", stdlibDependentSystemVersion)
newConfig = newConfig.replace("@stdlib.time.version@", stdlibDependentTimeVersion)
newConfig = newConfig.replace("@stdlib.uuid.version@", stdlibDependentUuidVersion)
ballerinaConfigFile.text = newConfig
}
}
Expand Down
81 changes: 81 additions & 0 deletions jwt-ballerina/client_self_signed_jwt_auth_provider.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) 2019 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/time;
import ballerina/uuid;

# Represents the client JWT Auth provider, which is used to authenticate with an external endpoint by generating
# a self signed JWT.
# ```ballerina
# jwt:ClientSelfSignedJwtAuthProvider provider = new({
# issuer: "example",
# audience: ["ballerina"],
# keyStoreConfig: {
# keyAlias: "ballerina",
# keyPassword: "ballerina",
# keyStore: {
# path: "/path/to/keystore.p12",
# password: "ballerina"
# }
# }
# });
# ```
public class ClientSelfSignedJwtAuthProvider {

IssuerConfig issuerConfig;

# Provides authentication based on the provided JWT configuration.
#
# + issuerConfig - JWT issuer configurations
public isolated function init(IssuerConfig issuerConfig) {
self.issuerConfig = issuerConfig;
}

# Generates the JWT for authentication.
# ```ballerina
# string|auth:Error token = provider.generateToken();
# ```
#
# + return - Generated token or else an `auth:Error` if token can't be generated
public isolated function generateToken() returns string|Error {
string|Error result = prepareJwtAuthToken(self.issuerConfig);
if (result is Error) {
return prepareError("Failed to generate JWT.", result);
}
return <string>result;
}
}

isolated function prepareJwtAuthToken(IssuerConfig issuerConfig) returns string|Error {
Header header = { alg: issuerConfig.signingAlg, typ: "JWT" };
Payload payload = {
sub: issuerConfig.username,
iss: issuerConfig.issuer,
exp: time:currentTime().time / 1000 + issuerConfig.expTimeInSeconds,
iat: time:currentTime().time / 1000,
nbf: time:currentTime().time / 1000,
jti: uuid:createType4AsString(),
aud: issuerConfig.audience
};

map<json>? customClaims = issuerConfig?.customClaims;
if (customClaims is map<json>) {
payload.customClaims = customClaims;
}

// TODO: cache the token per-user per-client and reuse it
return issue(header, payload, issuerConfig.keyStoreConfig);
}
125 changes: 0 additions & 125 deletions jwt-ballerina/inbound_jwt_auth_provider.bal

This file was deleted.

Loading

0 comments on commit 9c49984

Please sign in to comment.