From 2cad522ef55dd74d7ad92fca4717ae58755cb7d7 Mon Sep 17 00:00:00 2001 From: eson Date: Mon, 30 May 2022 18:43:28 +0800 Subject: [PATCH] init --- 1.json | 43 +++++++++++++++++++ 1.ldif | 32 ++++++++++++++ config.yaml | 5 +++ go.mod | 22 ++++++++++ go.sum | 28 +++++++++++++ main.go | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++ main_test.go | 7 ++++ 7 files changed, 253 insertions(+) create mode 100644 1.json create mode 100644 1.ldif create mode 100644 config.yaml create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 main_test.go diff --git a/1.json b/1.json new file mode 100644 index 0000000..95ca23a --- /dev/null +++ b/1.json @@ -0,0 +1,43 @@ +{ + "errcode": 0, + "errmsg": "ok", + "result": [ + { + "auto_add_user": true, + "create_dept_group": true, + "dept_id": 155809011, + "name": "总经理", + "parent_id": 1 + }, + { + "auto_add_user": true, + "create_dept_group": true, + "dept_id": 327788785, + "name": "财务部", + "parent_id": 1 + }, + { + "auto_add_user": true, + "create_dept_group": true, + "dept_id": 327970527, + "name": "行政人事部", + "parent_id": 1 + }, + { + "auto_add_user": true, + "create_dept_group": true, + "dept_id": 328448383, + "ext": "{\"faceCount\":\"2\"}", + "name": "技术研发部", + "parent_id": 1 + }, + { + "auto_add_user": true, + "create_dept_group": true, + "dept_id": 328731271, + "name": "销售部", + "parent_id": 1 + } + ], + "request_id": "16m7bncjtxiwt" +} \ No newline at end of file diff --git a/1.ldif b/1.ldif new file mode 100644 index 0000000..2fc21af --- /dev/null +++ b/1.ldif @@ -0,0 +1,32 @@ +version: 3 + +dn: ou=members,dc=yuandian,dc=com +objectClass: organizationalUnit +objectClass: top +ou: members + +dn:: Y2495Lil5bO7LG91PW1lbWJlcnMsZGM9eXVhbmRpYW4sZGM9Y29t +objectClass: inetOrgPerson +objectClass: organizationalPerson +objectClass: person +objectClass: top +cn:: 5Lil5bO7 +sn: eson +displayName:: 5Lil5bO7 +givenName:: 5Lil5bO7 +mail:: eWFuanVuQHl1YW5kaWFuLmNvbSA= +telephoneNumber: 185213213213 +userPassword:: e01ENX1wNlgwSFpRRnByeXhCZzZTZStsaldRPT0= + +dn:: Y2496buE5oCd5pWPLG91PW1lbWJlcnMsZGM9eXVhbmRpYW4sZGM9Y29t +objectClass: inetOrgPerson +objectClass: organizationalPerson +objectClass: person +objectClass: top +cn:: 6buE5oCd5pWP +sn: eson +displayName:: 6buE5oCd5pWP +givenName:: 6buE5oCd5pWP +mail:: aHVhbmdzaW1pbkB5dWFuZGlhbi5jb20g +telephoneNumber: 18588505404 +userPassword:: e01ENX14cTY1dDlucGZ5bUJxVmVnalJpMmd3PT0= \ No newline at end of file diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..1ba35e9 --- /dev/null +++ b/config.yaml @@ -0,0 +1,5 @@ + + +core: + appkey: + appsercert: \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..38d6baf --- /dev/null +++ b/go.mod @@ -0,0 +1,22 @@ +module yddingtalkldap + +go 1.18 + +require ( + github.com/go-ldap/ldap/v3 v3.4.3 + github.com/tidwall/gjson v1.14.1 +) + +require ( + github.com/Azure/go-ntlmssp v0.0.0-20211209120228-48547f28849e // indirect + github.com/go-asn1-ber/asn1-ber v1.5.4 // indirect + golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 // indirect +) + +require ( + github.com/474420502/requests v1.22.0 + github.com/474420502/structure v1.0.1 + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.0 // indirect + golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..23c1ff0 --- /dev/null +++ b/go.sum @@ -0,0 +1,28 @@ +github.com/474420502/random v0.5.2-0.20220222044003-09d6ed40ca23 h1:ZO9oDeD8EOHiHbFLPlZ5WyfF0uBoYfRD0/NoEIFUeAQ= +github.com/474420502/requests v1.22.0 h1:dRQczuYg3K3GlaQgm8SZQLmpiKc+jlOKzN0LkAKXUAo= +github.com/474420502/requests v1.22.0/go.mod h1:043PKfW//QR069XTYG5WT7t+z+d+8/C5PJtfWzpsf+o= +github.com/474420502/structure v1.0.1 h1:X8hM0m6CA+13HZI2/Uv3pO+Kce3cm/ZGfXVeE+S4uN8= +github.com/474420502/structure v1.0.1/go.mod h1:rND3ZSiZH84JKV43+bR0/iUHvLlhkcBmW9hyUNkr3bU= +github.com/Azure/go-ntlmssp v0.0.0-20211209120228-48547f28849e h1:ZU22z/2YRFLyf/P4ZwUYSdNCWsMEI0VeyrFoI2rAhJQ= +github.com/Azure/go-ntlmssp v0.0.0-20211209120228-48547f28849e/go.mod h1:chxPXzSsl7ZWRAuOIE23GDNzjWuZquvFlgA8xmpunjU= +github.com/elazarl/goproxy v0.0.0-20210801061803-8e322dfb79c4 h1:lS3P5Nw3oPO05Lk2gFiYUOL3QPaH+fRoI1wFOc4G1UY= +github.com/go-asn1-ber/asn1-ber v1.5.4 h1:vXT6d/FNDiELJnLb6hGNa309LMsrCoYFvpwHDF0+Y1A= +github.com/go-asn1-ber/asn1-ber v1.5.4/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0= +github.com/go-ldap/ldap/v3 v3.4.3 h1:JCKUtJPIcyOuG7ctGabLKMgIlKnGumD/iGjuWeEruDI= +github.com/go-ldap/ldap/v3 v3.4.3/go.mod h1:7LdHfVt6iIOESVEe3Bs4Jp2sHEKgDeduAhgM1/f9qmo= +github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo= +github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 h1:tkVvjkPTB7pnW3jnid7kNyAMPVWllTNOf/qKDze4p9o= +golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 h1:CIJ76btIcR3eFI5EgSo6k1qKw9KJexJuRLI9G7Hp5wE= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..3adceb2 --- /dev/null +++ b/main.go @@ -0,0 +1,116 @@ +package main + +import ( + "crypto/md5" + "encoding/base64" + "encoding/json" + "fmt" + "log" + + "github.com/474420502/requests" + arraystack "github.com/474420502/structure/stack/array" + "github.com/go-ldap/ldap/v3" + "github.com/tidwall/gjson" +) + +var appkey = "dingwzebpctav6l3bvkn" +var appsecret = "M_WTqBDk06IS6maj_EHipsvYQe76Led72dTKqceHnVJELJ44KOyG1qV5O6MeX2e_" + +type Department struct { + DepartID int64 + Name string + DN []string + Sub []*Department + Member gjson.Result +} + +func main() { + + ldapconn, err := ldap.DialURL("ldap://ldap.yuandian.com:389") + if err != nil { + log.Fatal(err) + } + defer ldapconn.Close() + + err = ldapconn.Bind("cn=admin,dc=yuandian,dc=com", "yuandianldap123") + if err != nil { + log.Fatal(err) + } + + ses := requests.NewSession() + tp := ses.Get(fmt.Sprintf("https://oapi.dingtalk.com/gettoken?appkey=%s&appsecret=%s", appkey, appsecret)) + resp, err := tp.Execute() + if err != nil { + panic(err) + } + var rjson map[string]any + if err := json.Unmarshal(resp.Content(), &rjson); err != nil { + panic(err) + } + token := rjson["access_token"] + log.Println(token) + + Root := &Department{DepartID: 1, DN: []string{"dn=yuandian", "dn=com"}} + + stack := arraystack.New[*Department]() + stack.Push(Root) + + for !stack.Empty() { + department, _ := stack.Pop() + + // 获取子部门信息 + tp = ses.Post(fmt.Sprintf("https://oapi.dingtalk.com/topapi/v2/department/listsub?access_token=%s", token)) + if department.DepartID > 1 { + tp.SetBodyAuto(fmt.Sprintf(`{"dept_id": %d}`, department.DepartID)) + } + resp, err = tp.Execute() + if err != nil { + panic(err) + } + + listsub := gjson.ParseBytes(resp.Content()).Get("result").Array() + for _, sub := range listsub { + var dept = &Department{ + DepartID: sub.Get("dept_id").Int(), + Name: sub.Get("name").String(), + } + stack.Push(dept) + department.Sub = append(department.Sub, dept) + } + + tp = ses.Post(fmt.Sprintf("https://oapi.dingtalk.com/topapi/v2/user/list?access_token=%s", token)) + tp.SetBodyAuto(fmt.Sprintf(`{"dept_id": %d, "cursor": 0, "size": 100}`, department.DepartID)) + resp, err = tp.Execute() + if err != nil { + panic(err) + } + + department.Member = gjson.ParseBytes(resp.Content()).Get("result.list") + + if department.DepartID > 1 { + + req := ldap.NewAddRequest("cn=haha", nil) + req.Attribute("objectClass", []string{"inetOrgPerson", "organizationalPerson", "person", "top"}) + req.Attribute("cn", []string{"haha"}) + req.Attribute("sn", []string{"eson"}) + req.Attribute("displayName", []string{"haha"}) + req.Attribute("givenName", []string{"haha"}) + req.Attribute("mail", []string{"haha"}) + req.Attribute("telephoneNumber", []string{"18588505404"}) + md5hash := md5.New() + md5hash.Write([]byte("18588505404")) + pwd := base64.StdEncoding.EncodeToString(md5hash.Sum(nil)) + log.Println(pwd) + req.Attribute("userPassword", []string{"{MD5}" + pwd}) + + err = ldapconn.Add(req) + if err != nil { + panic(err) + } + } + + log.Println(string(resp.Content())) + } + + log.Println(Root) +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..6f4278a --- /dev/null +++ b/main_test.go @@ -0,0 +1,7 @@ +package main + +import "testing" + +func TestCase1(t *testing.T) { + main() +}