Code Monkey home page Code Monkey logo

go-ad-auth's Introduction

pkg.go.dev

About

go-ad-auth is a simple wrapper around the great ldap library to help with Active Directory authentication.

Installing

Using Go Modules:

go get github.com/korylprince/go-ad-auth/v3

Using gopkg.in:

go get gopkg.in/korylprince/go-ad-auth.v3

Dependencies:

If you have any issues or questions create an issue.

API Versions

You should update to the v3 API when possible. The new API is cleaner, more idiomatic, exposes a lot more functionality, and is fully testable.

v3 was created to support Go Modules, so it is backwards compatible with v2. However, updates made to v3 are not backported to v2.

The v3 API is almost a complete rewrite of the older gopkg.in/korylprince/go-ad-auth.v1 API. There are similarities, but v3 is not backwards-compatible.

One notable difference to be careful of is that while v1's Login will return false if the user is not in the specified group, v3's AuthenticateExtended will return true if the user authenticated successfully, regardless if they were in any of the specified groups or not.

Usage

Example:

config := &auth.Config{
    Server:   "ldap.example.com",
    Port:     389,
    BaseDN:   "OU=Users,DC=example,DC=com",
    Security: auth.SecurityStartTLS,
}

username := "user"
password := "pass"

status, err := auth.Authenticate(config, username, password)

if err != nil {
    //handle err
    return
}

if !status {
    //handle failed authentication
    return
}

See more advanced examples on go.dev.

Testing

go test -v

Most tests will be skipped unless you supply the following environment variables to connect to an Active Directory server:

Name Description
ADTEST_SERVER Hostname or IP Address of an Active Directory server
ADTEST_PORT Port to use - defaults to 389
ADTEST_BIND_UPN userPrincipalName ([email protected]) of admin user
ADTEST_BIND_PASS Password of admin user
ADTEST_BIND_SECURITY NONE || TLS || STARTTLS || INSECURETLS || INSECURESTARTTLS - defaults to STARTTLS
ADTEST_BASEDN LDAP Base DN - for testing the root DN is recommended, e.g. DC=example,DC=com
ADTEST_PASSWORD_UPN userPrincipalName of a test user that will be used to test password changing functions

Nested Groups

Since v3.1.0, AuthenticateExtended and Conn.ObjectGroups will automatically search for nested groups. For example, if User A is a member of Group A, and Group A is a member of Group B, using Conn.ObjectGroups on User A will return both Group A and Group B.

Security

SQL Injection is a well known attack vector, and most SQL libraries provide mitigations such as prepared statements. Similarly, LDAP Injection, while not seen often in the wild, is something we should be concerned with.

Since v2.2.0, this library sanitizes inputs (with ldap.EscapeFilter) that are used to create LDAP filters in library functions, namely GetDN and GetAttributes. This means high level functions in this library are protected against malicious inputs. If you use Search or SearchOne, take care to sanitize any untrusted inputs you use in your LDAP filter.

go-ad-auth's People

Contributors

korylprince avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-ad-auth's Issues

Authenticates multiple passwords

Hi

If i am logged in to application, and I change my existing password. Then after i relogin, it authenticates both the passwords(New one and the existing one). I think it holds all the Active Directory data.
Please help with this problem.

This is my code.

--->
logger.Log(MODULENAME, logger.DEBUG, "LDAP Authentication Ready To begin...!!")

Config1 := grepo.GetGlobalConfiguration()
//&auth.Config{} = Config.EnvConfig.UserAuthenticationConfigParams.

config := &auth.Config{
	Server:   Config1.EnvConfig.UserAuthenticationConfigParams.Server,//example-ad.example.com
	Port:     Config1.EnvConfig.UserAuthenticationConfigParams.Port,//389
	BaseDN:   Config1.EnvConfig.UserAuthenticationConfigParams.BaseDN, //"DC=example,DC=COM",
	Security: auth.SecurityNone,
	Debug:    Config1.EnvConfig.UserAuthenticationConfigParams.Debug, //true,
}

status, err := auth.Login(userName, password, "", config)
if err != nil {
	logger.Log(MODULENAME, logger.ERROR, "Failed to Perform Authentication")
	logger.Log(MODULENAME, logger.ERROR, "Error : %s \n", err.Error())
}
return status

Escape LDAP filter arguments

Hi,

When entry.DN contains a backslash, a parenthesis, an asterisk or a null character, the following query fails because the filter is invalid.

foundGroups, err := conn.Search(fmt.Sprintf("(member:%s:=%s)", LDAPMatchingRuleInChain, entry.DN), []string{""}, 1000)

You should escape entry.DN using the rules detailed here, paragraph "The String Representation of LDAP Filters": https://ldap.com/ldap-filters/

Wrong keyword

In AuthenticateExtended when searching the memberOf attribute for matching groups, I think you meant to break out of the inner loop when you detect a match rather than continue.

continue

Continuing doesn't hurt, but it also doesn't do anything useful :-)

LDAP Error : Amount Of Entries Returned was not one.

Hey there,

I am trying to authenticate a particular user. But, the error message shows "Amount Of Entries Returned was not one." All other users in the same domain are able to log in,but not this user.
This user's name is 17characters long. Does this have any dependency on length of characters?
Please help me with this issue.

Thanks

Unable to change user password if TLS is not enabled in domain

func ExampleUpdatePassword() {
	config := &auth.Config{
		Server:   "ldap.example.com",
		Port:     389,
		BaseDN:   "OU=Users,DC=example,DC=com",
		Security: auth.SecurityNone,
	}

	username := "user"
	password := "pass"
	newPassword := "Super$ecret"

	if err := auth.UpdatePassword(config, username, password, newPassword); err != nil {
		//handle err
	}
}

Result is
LDAP Result Code 53 "Unwilling To Perform": 00002077: SvcErr: DSID-03190EBE, problem 5003 (WILL_NOT_PERFORM)

Connection error: LDAP Result Code 2 "Protocol Error": unsupported extended operation

Hello,

When trying connection to LDAP server setup on linux, facing connection error.

Error: "Connection error: LDAP Result Code 2 "Protocol Error": unsupported extended operation"

Note that with same client, we are able to connect to LDAP on Windows server.

Configs/versions we are using are as below:

Client OS Details:

NAME=Buildroot
VERSION=2015.11-00069-g85a7c28e
ID=buildroot
VERSION_ID=2015.11
PRETTY_NAME="Buildroot 2015.11"
MVEBU_RELEASE="16.08.0"

Linux OS details on which LDAP is setup:

NAME="Ubuntu"
VERSION="18.04.4 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.4 LTS"
VERSION_ID="18.04"

OpenLDAP Version:
Version: 2.4.45+dfsg-1ubuntu1.11

Using golang v3.1.4 version of go-ad-auth:
https://pkg.go.dev/github.com/korylprince/go-ad-auth/[email protected]

Using this with Port 389 and SecurityType SecurityInsecureStartTLS for LDAP connection.
Note that port is open, no issues with firewall

API function call being used is: https://pkg.go.dev/github.com/korylprince/go-ad-auth/[email protected]#AuthenticateExtended

Could you please advice on this issue? Is it something with respect to versions we are using?

Get user groups

Hi,

I'm having some trouble getting the groups that a user is a member of from "AuthenticateExtended". No matter what I pass in attr and group, I always get back an error "no entries returned". Is this a bug?

BTW: I can successfully authenticate with the "Authenticate" function.

Thanks,
-G

adauth.AuthenticateExtended call is not returning an expected ldap group

Hello,

I created a new ldap user in Windows Active Directory Users and Computers. When this new user is created by default it is a member of 'Domain Users' group. But when I used adauth.AuthenticateExtended call it did not return this group.

Then I created a new group say 'DCS' and made this new user member of 'DCS', then ideally the user is member of 'Domain Users' and 'DCS'. But the adauth.AuthenticateExtended call returned only 'DCS'. Note that I had passed both groups in the call.

After more investigation, in LDAP server I noticed that attribute 'member' of 'DCS' group had reference to the new user, but the 'member' attribute of 'Domain User' did not have reference to the user.

In Windows Active Directory though it says user is member of both 'Domain User' and 'DCS', the AuthenticateExtended call returns only DCS, as attribute 'member' of 'Domain Users' does not mention this user.

So,
does this library check the attributes before returning the group? or
By default, as user is not referred in 'Domain Users', is it LDAP configuration issue?

checking other users username and password

Hi

Thanks for this great package! I would like to use this to check users username, password and group membership.

I have got this working when I use AuthenticateExtended. This works fine when a privileged user logs in, it pulls the attributes and groups. But when a standard user attempts I get "no entries returned".

So I assume I will need to first do a bind and use another method to check the user. Is there a way to be able to do this within this package or do I need to use the ldap package?

Many thanks

Simon

Unable to change password with TLS not enabled in domain

func ExampleUpdatePassword() {
	config := &auth.Config{
		Server:   "ldap.example.com",
		Port:     389,
		BaseDN:   "OU=Users,DC=example,DC=com",
		Security: auth.SecurityNone,
	}

	username := "user"
	password := "pass"
	newPassword := "Super$ecret"

	if err := auth.UpdatePassword(config, username, password, newPassword); err != nil {
		//handle err
	}
}

Result is
LDAP Result Code 53 "Unwilling To Perform": 0000001F: SvcErr: DSID-031A12D2, problem 5003 (WILL_NOT_PERFORM)

Authentication on LDAP over TLS does not work

Hello.

When I trying to authenticate via TLS on the server via TLS (port 636) - it throws an error:

panic: Connection error: LDAP Result Code 200 "Network Error": x509: cannot validate certificate for X.X.X.X because it doesn't contain any IP SANs.

My config:

config := &auth.Config{
	Server: "X.X.X.X",
	Port: 636,
	BaseDN: "ou=Users,dc=my,dc=example,dc=com",
	Security: auth.SecurityTLS,
}

The one way to avoid this error is change call of the function DialTLS on line 27 from

conn, err := ldap.DialTLS("tcp", fmt.Sprintf("%s:%d", c.Server, c.Port), &tls.Config{ServerName: c.Server})

to

conn, err := ldap.DialTLS("tcp", fmt.Sprintf("%s:%d", c.Server, c.Port), &tls.Config{ServerName: c.Server, InsecureSkipVerify: true})

This method is not so good, because in InsecureScipVerify mode we are susceptible to man-in-the-middle attacks.

Is there any way to fix it without using bad practices? Maybe I made mistake somewhere?

ObjectGroups

Hello!
I could not understand which parameters i should sent.
User dn and his exist group dn?
("dn", "user dn", []string{"group dn"})
This is return empty groups.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.