Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed bugs and test case fixing #66

Merged
merged 1 commit into from
Jan 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions components/auth/org.wso2.carbon.auth.oauth/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
<groupId>org.wso2.carbon.auth</groupId>
<artifactId>org.wso2.carbon.auth.user.mgt</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.auth</groupId>
<artifactId>org.wso2.carbon.auth.client.registration</artifactId>
</dependency>
<dependency>
<groupId>com.nimbusds</groupId>
<artifactId>oauth2-oidc-sdk</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
public class JDBCAuthConstants {
public static final String TOKEN_ID = "TOKEN_ID";
public static final String CLIENT_ID = "CLIENT_ID";
public static final String ACCESS_TOKEN = "ACCESS_TOKEN";
public static final String REFRESH_TOKEN = "REFRESH_TOKEN";
public static final String CONSUMER_KEY_ID = "CONSUMER_KEY_ID";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,11 @@ public AccessTokenDTO getTokenInfoByID(String tokenID) throws SQLException {
public AccessTokenDTO getTokenInfo(String accessToken) throws SQLException {
log.debug("Calling getTokenInfo for accessToken");
AccessTokenDTO accessTokenDTO = new AccessTokenDTO();
final String query = "SELECT TOKEN_ID, ACCESS_TOKEN, REFRESH_TOKEN, CONSUMER_KEY_ID, AUTHZ_USER, "
final String query = "SELECT TOKEN_ID, ACCESS_TOKEN, REFRESH_TOKEN, CLIENT_ID, AUTHZ_USER, "
+ "TIME_CREATED, REFRESH_TOKEN_TIME_CREATED, VALIDITY_PERIOD, REFRESH_TOKEN_VALIDITY_PERIOD, "
+ "TOKEN_SCOPE_HASH, TOKEN_STATE, USER_TYPE, GRANT_TYPE FROM AUTH_ACCESS_TOKEN WHERE "
+ "ACCESS_TOKEN = ? ";
+ "TOKEN_SCOPE_HASH, TOKEN_STATE, USER_TYPE, GRANT_TYPE FROM AUTH_ACCESS_TOKEN INNER JOIN "
+ "AUTH_OAUTH2_APPLICATIONS WHERE ACCESS_TOKEN = ? AND AUTH_ACCESS_TOKEN.CONSUMER_KEY_ID = "
+ "AUTH_OAUTH2_APPLICATIONS.ID";

try (Connection connection = DAOUtil.getAuthConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
Expand All @@ -133,7 +134,7 @@ public AccessTokenDTO getTokenInfo(String accessToken) throws SQLException {
accessTokenDTO.setTokenID(rs.getInt(JDBCAuthConstants.TOKEN_ID));
accessTokenDTO.setAccessToken(rs.getString(JDBCAuthConstants.ACCESS_TOKEN));
accessTokenDTO.setRefreshToken(rs.getString(JDBCAuthConstants.REFRESH_TOKEN));
accessTokenDTO.setConsumerKey(rs.getString(JDBCAuthConstants.CONSUMER_KEY_ID));
accessTokenDTO.setConsumerKey(rs.getString(JDBCAuthConstants.CLIENT_ID));
accessTokenDTO.setAuthUser(rs.getString(JDBCAuthConstants.AUTHZ_USER));
accessTokenDTO.setTimeCreated(rs.getTimestamp(JDBCAuthConstants.TIME_CREATED).getTime());
accessTokenDTO.setRefreshTokenCreatedTime(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.auth.client.registration.ClientRegistrationHandler;
import org.wso2.carbon.auth.client.registration.impl.ClientRegistrationFactory;
import org.wso2.carbon.auth.client.registration.model.Application;
import org.wso2.carbon.auth.core.test.common.AuthDAOIntegrationTestBase;
//import org.wso2.carbon.auth.oauth.IntegrationTestBase;
import org.wso2.carbon.auth.oauth.dto.AccessTokenDTO;
Expand All @@ -37,15 +40,27 @@ public class TokenManagerImplTest extends AuthDAOIntegrationTestBase {
public void setup() throws Exception {
super.init();
super.setup();
log.info("setup AuthRequestHandlerTest");
log.info("setup TokenManagerImplTest");
}

@Test
public void testGetTokenInfo() throws Exception {
ClientRegistrationHandler defaultClientRegistrationHandler = ClientRegistrationFactory.getInstance()
.getClientRegistrationHandler();
Application application = new Application();
application.setClientName("tokenGet-app");
application.setClientId(UUID.randomUUID().toString());
application.setClientSecret(UUID.randomUUID().toString());
application.setGrantTypes("password");
application.setCallBackUrl("http://localhost/callback");
application.setOauthVersion("2");
defaultClientRegistrationHandler.registerApplication(application);


TokenManagerImpl tokenManager = new TokenManagerImpl();
String accessToken = UUID.randomUUID().toString();
String refreshToken = UUID.randomUUID().toString();
String clientID = "sample";
String clientID = application.getClientId();
String authUser = "admin";
String userDomain = "default";
long timeCreated = Calendar.getInstance().getTimeInMillis();
Expand All @@ -55,7 +70,8 @@ public void testGetTokenInfo() throws Exception {
String tokenScopeHash = "hash";
String tokenState = "active";
String userType = "application user";
String grantType = "password";
String grantType = application.getGrantTypes();

tokenManager.storeToken(accessToken, refreshToken, clientID, authUser, userDomain, timeCreated,
refreshTokenCreatedTime, validityPeriod, refreshTokenValidityPeriod, tokenScopeHash, tokenState,
userType, grantType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@
org.apache.commons.lang3.*; version="${apache.commons.lang3.import.version.range}",
com.nimbusds.oauth2.sdk.*;version="${com.nimbusd.oauth2-oidc-sdk.import.version.range}",
javax.ws.rs.*; version="${javax.ws.rs.import.version.range}",
org.wso2.carbon.auth.token.introspection.*; version="${carbon.auth.version}"
org.wso2.carbon.auth.token.introspection.*; version="${carbon.auth.version}",
com.google.gson.annotations.*,
com.google.gson.*,
</import.package>
<export.package>
org.wso2.carbon.auth.token.introspection.rest.api.*; version="${carbon.auth.version}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.google.gson.annotations.SerializedName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.Objects;
Expand All @@ -23,7 +24,7 @@ public class IntrospectionResponseDTO {
@JsonProperty("token_type")
private String tokenType = null;

@JsonProperty("client_id")
@SerializedName("client_id")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #67

private String clientId = null;

@JsonProperty("exp")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.wso2.carbon.auth.token.introspection.rest.api.impl;

import com.google.gson.Gson;
import org.wso2.carbon.auth.token.introspection.IntrospectionManager;
import org.wso2.carbon.auth.token.introspection.dto.IntrospectionResponse;
import org.wso2.carbon.auth.token.introspection.impl.IntrospectionManagerImpl;
Expand All @@ -19,6 +20,8 @@ public Response introspect(String token, Request request) throws NotFoundExcepti
IntrospectionResponse introspectionResponse = introspectionManager.introspect(token);
IntrospectionResponseDTO introspectionResponseDTO = MappingUtil
.applicationModelToApplicationDTO(introspectionResponse);
return Response.status(Response.Status.OK).entity(introspectionResponseDTO).build();
Gson gson = new Gson();
String json = gson.toJson(introspectionResponseDTO);
return Response.status(Response.Status.OK).entity(json).build();
Copy link
Contributor

@malinthaprasan malinthaprasan Jan 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #68

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.wso2.carbon.auth.token.introspection.rest.api.impl;

import com.google.gson.Gson;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -60,13 +61,16 @@ public void testIntrospect() throws Exception {
PowerMockito.when(context.getIntrospectionResponse()).thenReturn(introspectionResponse);

Response response = service.introspect("ASDF5F6sd587fdgfsakjdd", null);
IntrospectionResponseDTO dto = (IntrospectionResponseDTO) response.getEntity();
String payload = (String) response.getEntity();
Gson gson = new Gson();
IntrospectionResponseDTO dto = gson.fromJson(payload, IntrospectionResponseDTO.class);
Assert.assertTrue(dto.getActive());

PowerMockito.when(tokenValidatorHandler, "validate", Mockito.any(IntrospectionContext.class))
.thenThrow(IntrospectionException.class);
response = service.introspect("inactive", null);
dto = (IntrospectionResponseDTO) response.getEntity();
payload = (String) response.getEntity();
dto = gson.fromJson(payload, IntrospectionResponseDTO.class);
Assert.assertFalse(dto.getActive());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void validate(IntrospectionContext context) throws IntrospectionException
// token scopes
introspectionResponse.setScope("");
// set user-name
introspectionResponse.setUsername("");
introspectionResponse.setUsername("admin");
Copy link
Contributor

@malinthaprasan malinthaprasan Jan 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #69

// add client id
introspectionResponse.setClientId(accessTokenDTO.getConsumerKey());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2018, 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.
*/
package org.wso2.carbon.auth.user.mgt;

/**
* Constants for user mgt component
*/
public class Constants {
public static final String WSO2_UM_DB = "WSO2_UM_DB";
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.auth.core.configuration.models.UserStoreConfiguration;
import org.wso2.carbon.auth.user.mgt.Constants;
import org.wso2.carbon.auth.user.mgt.UserStoreException;
import org.wso2.carbon.auth.user.mgt.UserStoreManager;
import org.wso2.carbon.auth.user.store.connector.PasswordHandler;
Expand All @@ -46,7 +47,7 @@ public JDBCUserStoreManager() {
UserStoreConnector connector = UserStoreConnectorFactory.getUserStoreConnector();
UserStoreConfiguration config = new UserStoreConfiguration();
Map<String, Object> properties = new HashMap<>();
properties.put(JDBCConnectorConstants.DATA_SOURCE, "WSO2UM_DB");
properties.put(JDBCConnectorConstants.DATA_SOURCE, Constants.WSO2_UM_DB);
properties.put(JDBCConnectorConstants.DATABASE_TYPE, "MySql");
config.setProperties(properties);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class UserStoreConstants {
public static final String ITERATION_COUNT = "iteration_count";
public static final String KEY_LENGTH = "key_length";
public static final String CLAIM_USERNAME = "urn:ietf:params:scim:schemas:core:2.0:User:userName";

public static final String PASSWORD_URI = "password";
public static final String DATASOURCE_WSO2UM_DB = "WSO2_UM_DB";

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,31 @@

package org.wso2.carbon.auth.user.store.internal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.auth.core.configuration.models.UserStoreConfiguration;
import org.wso2.carbon.auth.user.store.connector.Attribute;
import org.wso2.carbon.auth.user.store.connector.UserStoreConnector;
import org.wso2.carbon.auth.user.store.connector.UserStoreConnectorFactory;
import org.wso2.carbon.auth.user.store.constant.JDBCConnectorConstants;
import org.wso2.carbon.auth.user.store.constant.UserStoreConstants;
import org.wso2.carbon.auth.user.store.exception.UserStoreConnectorException;
import org.wso2.carbon.datasource.core.api.DataSourceService;
import org.wso2.carbon.datasource.core.exception.DataSourceException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.security.auth.callback.PasswordCallback;
import javax.sql.DataSource;


/**
* Database related utility methods.
*/
public class ConnectorDataHolder {

private static Logger log = LoggerFactory.getLogger(ConnectorDataHolder.class);
private static final ConnectorDataHolder instance = new ConnectorDataHolder();

private DataSourceService dataSourceService;
Expand Down Expand Up @@ -55,6 +70,32 @@ public DataSource getDataSource(String dataSourceName) throws DataSourceExceptio

public void setDataSourceService(DataSourceService dataSourceService) {
this.dataSourceService = dataSourceService;

//adding default admin user
char[] password = "admin".toCharArray();
String user = "admin";
UserStoreConnector connector = UserStoreConnectorFactory.getUserStoreConnector();
UserStoreConfiguration config = new UserStoreConfiguration();
Map<String, Object> properties = new HashMap<>();
properties.put(JDBCConnectorConstants.DATA_SOURCE, UserStoreConstants.DATASOURCE_WSO2UM_DB);
properties.put(JDBCConnectorConstants.DATABASE_TYPE, "MySql");
config.setProperties(properties);
try {
connector.init(config);
} catch (UserStoreConnectorException e) {
log.error("Error occurred while init UserStoreConnector", e);
}
List<Attribute> attributeList = new ArrayList<>();
attributeList.add(new Attribute(UserStoreConstants.CLAIM_USERNAME, user));
try {
String userId = connector.addUser(attributeList);
PasswordCallback passwordCallback = new PasswordCallback(UserStoreConstants.PASSWORD_URI, false);
passwordCallback.setPassword(password);
connector.addCredential(userId, passwordCallback);
log.info("added default user admin");
} catch (UserStoreConnectorException e) {
log.error("Error adding admin user", e);
}
}

/* public void setPasswordHandler(String name, PasswordHandler passwordHandler) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ public class Constants {
public static final String DISPLAY_NAME_URI = "urn:ietf:params:scim:schemas:core:2.0:Group:displayName";
public static final String USERNAME_URI = "urn:ietf:params:scim:schemas:core:2.0:User:userName";
public static final String PASSWORD_URI = "password";
public static final String DATASOURCE_WSO2UM_DB = "WSO2UM_DB";
public static final String DATASOURCE_WSO2UM_DB = "WSO2_UM_DB";
}
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,20 @@ public void testListConnectorUserIds() throws Exception {

@Test
public void testGetUserAttributeValues() throws Exception {
// add test user
List<Attribute> attributeList = new ArrayList<>();
String adminUser = "superAdmin";
String adminPass = "superAdminPass";
attributeList.add(new Attribute(Constants.USERNAME_URI, adminUser));
attributeList.add(new Attribute(Constants.PASSWORD_URI, adminPass));
String adminUserId = connector.addUser(attributeList);
Assert.assertNotNull(adminUserId);

String userId = null;
try {
userId = connector.getConnectorUserId(UserStoreConstants.CLAIM_USERNAME, "admin");
userId = connector.getConnectorUserId(UserStoreConstants.CLAIM_USERNAME, adminUser);
Assert.assertNotNull(userId);
Assert.assertEquals(adminUserId, userId);
} catch (UserNotFoundException e) {
Assert.fail("Exception not expected");
}
Expand Down