Skip to content

Commit

Permalink
Fix lang-server test & use union for config
Browse files Browse the repository at this point in the history
  • Loading branch information
ayomawdb committed Feb 25, 2019
1 parent a8a4578 commit 5c581d9
Show file tree
Hide file tree
Showing 29 changed files with 92 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ballerina/log;
http:Client httpEndpoint = new("https://localhost:9090", config = {
auth: {
scheme: http:BASIC_AUTH,
basicAuthConfig: {
config: {
username: "tom",
password: "1234"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function main() {
// Create a JWT authentication provider with the relevant configurations.
http:AuthProvider jwtAuthProvider = {
scheme: http:JWT_AUTH,
jwtAuthProviderConfig: {
config: {
issuer: "ballerina",
audience: ["ballerina.io"],
certificateAlias: "ballerina",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ballerina/log;
http:Client httpEndpoint = new("https://www.googleapis.com/tasks/v1", config = {
auth: {
scheme: http:OAUTH2,
oAuth2AuthConfig: {
config: {
accessToken: "ya29.GlufBimE7JZdiB_FpFtZn7p1WMtloVeMlqiYXDGF97068VvJCyK8rEFqBBkxT10E0qudipwxTjJTkU4we0hbOcHKjNTXz6JTEZYoRVn7F3-0O_bL9g71Rwek7TFI",
clientId: "833478926540-va43h2lhdhfc06i9eivlmaehl3o5uk1i.apps.googleusercontent.com",
clientSecret: "4ZsV4gwSuIoRdy6TKUXTanlw",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function testAuthSuccess() {
http:Client httpEndpoint = new("https://localhost:9090", config = {
auth: {
scheme: http:BASIC_AUTH,
basicAuthConfig: {
config: {
username: "tom",
password: "password1"
}
Expand All @@ -44,7 +44,7 @@ function testAuthnFailure() {
http:Client httpEndpoint = new("https://localhost:9090", config = {
auth: {
scheme: http:BASIC_AUTH,
basicAuthConfig: {
config: {
username: "tom",
password: "password"
}
Expand All @@ -65,7 +65,7 @@ function testAuthzFailure() {
http:Client httpEndpoint = new("https://localhost:9090", config = {
auth: {
scheme: http:BASIC_AUTH,
basicAuthConfig: {
config: {
username: "dick",
password: "password2"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ballerina/http;
// parameters.
http:AuthProvider jwtAuthProvider = {
scheme: http:JWT_AUTH,
jwtAuthProviderConfig: {
config: {
issuer:"ballerina",
audience: ["ballerina.io"],
certificateAlias: "ballerina",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"position": {
"line": 13,
"line": 15,
"character": 16
},
"source": "function/source/actionInvocationSuggestion1.bal",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"position": {
"line": 13,
"line": 15,
"character": 20
},
"source": "function/source/actionInvocationSuggestion2.bal",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"position": {
"line": 16,
"line": 18,
"character": 27
},
"source": "function/source/errorLiftingSuggestions1.bal",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ http:ClientEndpointConfig conf = {
url: "https://postman-echo.com/basic-auth",
auth: {
scheme: http:BASIC_AUTH,
basicAuthConfig: {
config: {
username: "postman",
password: "password"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ http:ClientEndpointConfig conf = {
url: "https://postman-echo.com/basic-auth",
auth: {
scheme: http:BASIC_AUTH,
basicAuthConfig: {
config: {
username: "postman",
password: "password"
}
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ http:ClientEndpointConfig conf = {
url: "https://postman-echo.com/basic-auth",
auth: {
scheme: http:BASIC_AUTH,
basicAuthConfig: {
config: {
username: "postman",
password: "password"
}
Expand Down
16 changes: 6 additions & 10 deletions stdlib/http/src/main/ballerina/http/client_endpoint.bal
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,10 @@ public type ProxyConfig record {
# AuthConfig record can be used to configure the authentication mechanism used by the HTTP endpoint.
#
# + scheme - Authentication scheme
# + basicAuthConfig - Configuration for BasicAuth scheme
# + oAuth2AuthConfig - Configuration for OAuth2 scheme
# + jwtAuthConfig - Configuration for JWT scheme. If inbound authentication is JWT, sends the same JWT with client
# invocation, unless reissuing is configured using InferredJwtIssuerConfig.
# + config - Configuration related to the selected authenticator.
public type AuthConfig record {
OutboundAuthScheme scheme;
BasicAuthConfig basicAuthConfig?;
OAuth2AuthConfig oAuth2AuthConfig?;
JwtAuthConfig jwtAuthConfig?;
BasicAuthConfig|OAuth2AuthConfig|JwtAuthConfig config?;
!...;
};

Expand All @@ -339,8 +334,8 @@ public type AuthConfig record {
# + username - Username for Basic authentication
# + password - Password for Basic authentication
public type BasicAuthConfig record {
string username = "";
string password = "";
string username;
string password;
!...;
};

Expand Down Expand Up @@ -374,7 +369,8 @@ public type OAuth2AuthConfig record {
#
# + inferredJwtIssuerConfig - JWT issuer configuration used to issue JWT with specific configuration
public type JwtAuthConfig record {
auth:InferredJwtIssuerConfig inferredJwtIssuerConfig?;
auth:InferredJwtIssuerConfig inferredJwtIssuerConfig;
!...;
};

function initialize(string serviceUrl, ClientEndpointConfig config) returns Client|error {
Expand Down
55 changes: 28 additions & 27 deletions stdlib/http/src/main/ballerina/http/http_secure_client.bal
Original file line number Diff line number Diff line change
Expand Up @@ -302,38 +302,37 @@ public function createHttpSecureClient(string url, ClientEndpointConfig config)
function generateSecureRequest(Request req, ClientEndpointConfig config) returns ()|error {
var auth = config.auth;
if (auth is AuthConfig) {
var authConfig = auth["config"];
if (auth.scheme == BASIC_AUTH) {
var basicAuthConfig = auth["basicAuthConfig"];
if (basicAuthConfig is ()) {
error e = error("Basic auth config not provided");
panic e;
} else {
string username = basicAuthConfig.username;
string password = basicAuthConfig.password;
if (authConfig is BasicAuthConfig) {
string username = authConfig.username;
string password = authConfig.password;
string str = username + ":" + password;
string token = encoding:encodeBase64(str.toByteArray("UTF-8"));
req.setHeader(AUTH_HEADER, AUTH_SCHEME_BASIC + WHITE_SPACE + token);
} else {
error e = error("Basic auth config not provided");
panic e;
}
} else if (auth.scheme == OAUTH2) {
var oAuth2AuthConfig = auth["oAuth2AuthConfig"];
if (oAuth2AuthConfig is ()) {
error e = error("OAuth2 config not provided");
panic e;
} else {
string accessToken = oAuth2AuthConfig.accessToken;
if (authConfig is OAuth2AuthConfig) {
string accessToken = authConfig.accessToken;
if (accessToken == EMPTY_STRING) {
return updateRequestAndConfig(req, config);
} else {
req.setHeader(AUTH_HEADER, AUTH_SCHEME_BEARER + WHITE_SPACE + accessToken);
}
} else {
error e = error("OAuth2 config not provided");
panic e;
}
} else if (auth.scheme == JWT_AUTH) {
var jwtAuthConfig = auth["jwtAuthConfig"];
string authToken = EMPTY_STRING;
if (jwtAuthConfig is ()) {
authToken = runtime:getInvocationContext().authenticationContext.authToken;
} else {
var jwtIssuerConfig = jwtAuthConfig["inferredJwtIssuerConfig"];
if (authConfig is OAuth2AuthConfig || authConfig is BasicAuthConfig) {
error e = error("JWT auth config not provided");
panic e;
} else if (authConfig is JwtAuthConfig) {
var jwtIssuerConfig = authConfig["inferredJwtIssuerConfig"];
if (jwtIssuerConfig is ()) {
authToken = runtime:getInvocationContext().authenticationContext.authToken;
} else {
Expand All @@ -360,6 +359,8 @@ function generateSecureRequest(Request req, ClientEndpointConfig config) returns
return token;
}
}
} else {
authToken = runtime:getInvocationContext().authenticationContext.authToken;
}
if (authToken == EMPTY_STRING) {
error err = error(HTTP_ERROR_CODE, { message: "JWT was not used during inbound authentication.
Expand All @@ -383,7 +384,7 @@ function generateSecureRequest(Request req, ClientEndpointConfig config) returns
function updateRequestAndConfig(Request req, ClientEndpointConfig config) returns ()|error {
string accessToken = check getAccessTokenFromRefreshToken(config);
req.setHeader(AUTH_HEADER, AUTH_SCHEME_BEARER + WHITE_SPACE + accessToken);
OAuth2AuthConfig? authConfig = config.auth.oAuth2AuthConfig;
var authConfig = config.auth.config;
if (authConfig is OAuth2AuthConfig) {
authConfig.accessToken = accessToken;
}
Expand All @@ -396,13 +397,13 @@ function updateRequestAndConfig(Request req, ClientEndpointConfig config) return
# + return - AccessToken received from the authorization server or `error` if error occured during HTTP client invocation
function getAccessTokenFromRefreshToken(ClientEndpointConfig config) returns string|error {
Client refreshTokenClient;
var oAuth2AuthConfig = config.auth.oAuth2AuthConfig;
if (oAuth2AuthConfig is OAuth2AuthConfig) {
string refreshToken = oAuth2AuthConfig.refreshToken;
string clientId = oAuth2AuthConfig.clientId;
string clientSecret = oAuth2AuthConfig.clientSecret;
string refreshUrl = oAuth2AuthConfig.refreshUrl;
string[] scopes = oAuth2AuthConfig.scopes;
var authConfig = config.auth.config;
if (authConfig is OAuth2AuthConfig) {
string refreshToken = authConfig.refreshToken;
string clientId = authConfig.clientId;
string clientSecret = authConfig.clientSecret;
string refreshUrl = authConfig.refreshUrl;
string[] scopes = authConfig.scopes;

if (refreshToken == EMPTY_STRING || clientId == EMPTY_STRING || clientSecret == EMPTY_STRING
|| refreshUrl == EMPTY_STRING) {
Expand All @@ -424,7 +425,7 @@ function getAccessTokenFromRefreshToken(ClientEndpointConfig config) returns str
if (scopeString != EMPTY_STRING) {
textPayload = textPayload + "&scope=" + scopeString.trim();
}
if (oAuth2AuthConfig.credentialBearer == AUTH_HEADER_BEARER) {
if (authConfig.credentialBearer == AUTH_HEADER_BEARER) {
string clientIdSecret = clientId + ":" + clientSecret;
refreshTokenRequest.addHeader(AUTH_HEADER, AUTH_SCHEME_BASIC + WHITE_SPACE +
encoding:encodeBase64(clientIdSecret.toByteArray("UTF-8")));
Expand Down
35 changes: 14 additions & 21 deletions stdlib/http/src/main/ballerina/http/service_endpoint.bal
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,12 @@ public type AuthCacheConfig record {
# + id - Authentication provider instance id
# + scheme - Authentication scheme
# + authStoreProvider - Authentication store provider (Config, LDAP, etc.) implementation
# + ldapAuthProviderConfig - LDAP auth provider related configurations
# + configAuthProviderConfig - Config auth provider related configurations
# + jwtAuthProviderConfig - JWT auth provider related configurations
# + config - Configuration related to the selected authentication provider.
public type AuthProvider record {
string id = "";
InboundAuthScheme? scheme = ();
AuthStoreProvider? authStoreProvider = ();
auth:LdapAuthProviderConfig? ldapAuthProviderConfig = ();
auth:ConfigAuthProviderConfig? configAuthProviderConfig = ();
auth:JWTAuthProviderConfig? jwtAuthProviderConfig = ();
auth:LdapAuthProviderConfig|auth:ConfigAuthProviderConfig|auth:JWTAuthProviderConfig? config = ();
!...;
};

Expand Down Expand Up @@ -293,21 +289,20 @@ function createAuthFiltersForSecureListener(ServiceEndpointConfiguration config,
auth:AuthStoreProvider authStoreProvider = new;

foreach var provider in authProviderList {
var authProviderConfig = provider.config;
if (provider.scheme == BASIC_AUTH) {
if (provider.authStoreProvider == LDAP_AUTH_STORE) {
var ldapAuthProviderConfig = provider.ldapAuthProviderConfig;
if (ldapAuthProviderConfig is auth:LdapAuthProviderConfig) {
auth:LdapAuthStoreProvider ldapAuthStoreProvider = new(ldapAuthProviderConfig, instanceId);
if (authProviderConfig is auth:LdapAuthProviderConfig) {
auth:LdapAuthStoreProvider ldapAuthStoreProvider = new(authProviderConfig, instanceId);
authStoreProvider = ldapAuthStoreProvider;
} else {
error e = error("LDAP auth provider config not provided");
panic e;
}
} else if (provider.authStoreProvider == CONFIG_AUTH_STORE) {
var configAuthProviderConfig = provider.configAuthProviderConfig;
auth:ConfigAuthStoreProvider configAuthStoreProvider;
if (configAuthProviderConfig is auth:ConfigAuthProviderConfig) {
configAuthStoreProvider = new(configAuthProviderConfig);
if (authProviderConfig is auth:ConfigAuthProviderConfig) {
configAuthStoreProvider = new(authProviderConfig);
} else {
configAuthStoreProvider = new({});
}
Expand All @@ -329,21 +324,20 @@ function createAuthFiltersForSecureListener(ServiceEndpointConfiguration config,
}

function createAuthHandler(AuthProvider authProvider, string instanceId) returns HttpAuthnHandler {
var authProviderConfig = authProvider.config;
if (authProvider.scheme == BASIC_AUTH) {
auth:AuthStoreProvider authStoreProvider = new;
if (authProvider.authStoreProvider == CONFIG_AUTH_STORE) {
var configAuthProviderConfig = authProvider.configAuthProviderConfig;
auth:ConfigAuthStoreProvider configAuthStoreProvider;
if (configAuthProviderConfig is auth:ConfigAuthProviderConfig) {
configAuthStoreProvider = new(configAuthProviderConfig);
if (authProviderConfig is auth:ConfigAuthProviderConfig) {
configAuthStoreProvider = new(authProviderConfig);
} else {
configAuthStoreProvider = new({});
}
authStoreProvider = configAuthStoreProvider;
} else if (authProvider.authStoreProvider == LDAP_AUTH_STORE) {
var ldapAuthProviderConfig = authProvider.ldapAuthProviderConfig;
if (ldapAuthProviderConfig is auth:LdapAuthProviderConfig) {
auth:LdapAuthStoreProvider ldapAuthStoreProvider = new(ldapAuthProviderConfig, instanceId);
if (authProviderConfig is auth:LdapAuthProviderConfig) {
auth:LdapAuthStoreProvider ldapAuthStoreProvider = new(authProviderConfig, instanceId);
authStoreProvider = ldapAuthStoreProvider;
} else {
error e = error("LDAP auth provider config not provided");
Expand All @@ -356,9 +350,8 @@ function createAuthHandler(AuthProvider authProvider, string instanceId) returns
HttpBasicAuthnHandler basicAuthHandler = new(authStoreProvider);
return basicAuthHandler;
} else if (authProvider.scheme == JWT_AUTH){
var jwtAuthProviderConfig = authProvider.jwtAuthProviderConfig;
if (jwtAuthProviderConfig is auth:JWTAuthProviderConfig) {
auth:JWTAuthProvider jwtAuthProvider = new(jwtAuthProviderConfig);
if (authProviderConfig is auth:JWTAuthProviderConfig) {
auth:JWTAuthProvider jwtAuthProvider = new(authProviderConfig);
HttpJwtAuthnHandler jwtAuthnHandler = new(jwtAuthProvider);
return jwtAuthnHandler;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import ballerina/http;
http:Client clientEP1 = new("https://localhost:9095/foo", config = {
auth: {
scheme: http:OAUTH2,
oAuth2AuthConfig: {
config: {
refreshToken: "5Aep861..zRMyCurAUgnwQaEjnCVqxK2utna7Mm4nb9UamD7BW50R2huecjSaLlv5mT1z_TViZ",
clientId: "3MVG9YDQS5WtC11paU2WcQjBB3L5w4gz52uriT8ksZ3nUVjKvrfQMrU4uvZohTftxStwNEW4cfStBEGRxRL68",
clientSecret: "9205371918321623741",
Expand All @@ -32,7 +32,7 @@ http:Client clientEP1 = new("https://localhost:9095/foo", config = {
http:Client clientEP2 = new("https://localhost:9095/foo", config = {
auth: {
scheme: http:OAUTH2,
oAuth2AuthConfig: {
config: {
refreshToken: "5Aep861..zRMyCurAUgnwQaEjnCVqxK2utna7Mm4nb9UamD7BW50R2huecjSaLlv5mT1z_TViZ",
clientId: "3MVG9YDQS5WtC11paU2WcQjBB3L5w4gz52uriT8ksZ3nUVjKvrfQMrU4uvZohTftxStwNEW4cfStBEGRxRL68",
clientSecret: "9205371918321623741",
Expand All @@ -45,7 +45,7 @@ http:Client clientEP2 = new("https://localhost:9095/foo", config = {
http:Client clientEP3 = new("https://localhost:9095/foo", config = {
auth: {
scheme: http:OAUTH2,
oAuth2AuthConfig: {
config: {
refreshToken: "5Aep861..zRMyCurAUgnwQaEjnCVqxK2utna7Mm4nb9UamD7BW50R2huecjSaLlv5mT1z_TViZ",
clientId: "3MVG9YDQS5WtC11paU2WcQjBB3L5w4gz52uriT8ksZ3nUVjKvrfQMrU4uvZohTftxStwNEW4cfStBEGRxRL68",
clientSecret: "9205371918321623741",
Expand All @@ -57,7 +57,7 @@ http:Client clientEP3 = new("https://localhost:9095/foo", config = {
http:Client clientEP4 = new("https://localhost:9095/foo", config = {
auth: {
scheme: http:OAUTH2,
oAuth2AuthConfig : {
config : {
refreshToken: "5Aep861..zRMyCurAUgnwQaEjnCVqxK2utna7Mm4nb9UamD7BW50R2huecjSaLlv5mT1z_TViZ",
clientId: "3MVG9YDQS5WtC11paU2WcQjBB3L5w4gz52uriT8ksZ3nUVjKvrfQMrU4uvZohTftxStwNEW4cfStBEGRxRL68",
clientSecret: "9205371918321623741",
Expand All @@ -70,7 +70,7 @@ http:Client clientEP4 = new("https://localhost:9095/foo", config = {
http:Client clientEP5 = new("https://localhost:9095/foo", config = {
auth: {
scheme: http:OAUTH2,
oAuth2AuthConfig: {
config: {
refreshToken: "5Aep861..zRMyCurAUgnwQaEjnCVqxK2utna7Mm4nb9UamD7BW50R2huecjSaLlv5mT1z_TViZ",
clientId: "3MVG9YDQS5WtC11paU2WcQjBB3L5w4gz52uriT8ksZ3nUVjKvrfQMrU4uvZohTftxStwNEW4cfStBEGRxRL68",
clientSecret: "9205371918321623741",
Expand Down
Loading

0 comments on commit 5c581d9

Please sign in to comment.