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
SUBMARINE-646. Create rest api to authenticate user from LDAP #419
Changes from 4 commits
a0335f3
137be29
311762e
11aef43
3d16805
767b717
3b860d2
a980f4a
ee7a45e
6169ee6
18f3233
60a53e9
e03047b
52b50b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF 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.apache.submarine.server.api.ldap; | ||
|
||
public class Ldap { | ||
private String username; | ||
private String password; | ||
|
||
public String getUsername() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please change |
||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please change |
||
this.username = username; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF 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.apache.submarine.server.api.spec; | ||
|
||
public class LdapSpec { | ||
|
||
/** | ||
* Name of the user | ||
*/ | ||
private String user_name; | ||
|
||
/** | ||
* password of the user | ||
*/ | ||
private String password; | ||
|
||
public String getUserName(){ | ||
return user_name; | ||
} | ||
|
||
public String getPassword(){ | ||
return password; | ||
} | ||
|
||
public void setUserName(String user_name) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. function setUserName param is |
||
this.user_name = user_name; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF 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.apache.submarine.server.ldap; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class LdapManager { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(LdapManager.class); | ||
|
||
private static volatile LdapManager manager; | ||
|
||
private final AtomicInteger experimentTemplateIdCounter = new AtomicInteger(0); | ||
|
||
|
||
/** | ||
* Get the singleton instance | ||
* @return object | ||
*/ | ||
public static LdapManager getInstance() { | ||
if (manager == null) { | ||
synchronized (LdapManager.class) { | ||
if (manager == null) { | ||
manager = new LdapManager(); | ||
} | ||
} | ||
} | ||
return manager; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,92 @@ | ||||||||
/* | ||||||||
* Licensed to the Apache Software Foundation (ASF) under one | ||||||||
* or more contributor license agreements. See the NOTICE file | ||||||||
* distributed with this work for additional information | ||||||||
* regarding copyright ownership. The ASF 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.apache.submarine.server.rest; | ||||||||
|
||||||||
import javax.naming.AuthenticationException; | ||||||||
import javax.naming.Context; | ||||||||
import javax.naming.NamingException; | ||||||||
import javax.naming.directory.DirContext; | ||||||||
import javax.naming.directory.InitialDirContext; | ||||||||
import javax.ws.rs.Consumes; | ||||||||
import javax.ws.rs.FormParam; | ||||||||
import javax.ws.rs.POST; | ||||||||
import javax.ws.rs.Path; | ||||||||
import javax.ws.rs.Produces; | ||||||||
import javax.ws.rs.core.MediaType; | ||||||||
import javax.ws.rs.core.Response; | ||||||||
|
||||||||
import org.apache.submarine.server.ldap.LdapManager; | ||||||||
|
||||||||
import java.util.Hashtable; | ||||||||
|
||||||||
|
||||||||
@Path(RestConstants.V1 + "/" + RestConstants.LDAP) | ||||||||
@Produces({MediaType.APPLICATION_JSON + "; " + RestConstants.CHARSET_UTF8}) | ||||||||
|
||||||||
public class LdapAuthenticateRestApi { | ||||||||
private final LdapManager ldapManager = LdapManager.getInstance(); | ||||||||
|
||||||||
@POST | ||||||||
@Produces(MediaType.APPLICATION_JSON) | ||||||||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | ||||||||
public Response authenticateUser(@FormParam("username") String username, | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please change param name and value name |
||||||||
@FormParam("password") String password) { | ||||||||
try { | ||||||||
//Authenticate the user using the credentials provided | ||||||||
authenticate(username, password); | ||||||||
|
||||||||
return Response.ok().build(); | ||||||||
} | ||||||||
catch (Exception e) { | ||||||||
return Response.status(Response.Status.FORBIDDEN).build(); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
private void authenticate(String username, String password) throws Exception { | ||||||||
DirContext ctx = null; | ||||||||
Hashtable<String, String> HashEnv = new Hashtable<>(); | ||||||||
|
||||||||
String loginId = "uid=" + username + ",dc=example,dc=com"; | ||||||||
|
||||||||
HashEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); | ||||||||
HashEnv.put(Context.SECURITY_PRINCIPAL, loginId); | ||||||||
HashEnv.put(Context.SECURITY_CREDENTIALS, password); | ||||||||
HashEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); | ||||||||
HashEnv.put("com.sun.jndi.ldap.connect.timeout", "3000"); | ||||||||
HashEnv.put(Context.PROVIDER_URL, "ldap://ldap.forumsys.com:389"); | ||||||||
|
||||||||
try { | ||||||||
ctx = new InitialDirContext(HashEnv); | ||||||||
} | ||||||||
catch (AuthenticationException e) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. code style
Suggested change
|
||||||||
e.printStackTrace(); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||||||||
} | ||||||||
|
||||||||
if (ctx != null) { | ||||||||
try { | ||||||||
ctx.close(); | ||||||||
} | ||||||||
catch (NamingException e) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
e.printStackTrace(); | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please change
username
touserName
.