Skip to content

Commit

Permalink
Merge pull request #4 from owncloud/feature/graph-me
Browse files Browse the repository at this point in the history
Add simple route for /me
  • Loading branch information
tboerger authored Dec 5, 2019
2 parents 235e9f5 + 298f03b commit de28802
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ require (
github.com/restic/calens v0.1.0 // indirect
github.com/spf13/viper v1.5.0
github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce
github.com/yaegashi/msgraph.go v0.0.0-20191104022859-3f9096c750b2
go.opencensus.io v0.22.2
golang.org/x/net v0.0.0-20191126235420-ef20fe5d7933
google.golang.org/genproto v0.0.0-20191108220845-16a3f7862a1a
gopkg.in/ldap.v3 v3.1.0
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,8 @@ github.com/xeipuuv/gojsonschema v1.1.0/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4m
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/yaegashi/msgraph.go v0.0.0-20191104022859-3f9096c750b2 h1:37LbK2gAU+1oaWKC5NTz+fNOsR2LgdRj/SAFVMucgss=
github.com/yaegashi/msgraph.go v0.0.0-20191104022859-3f9096c750b2/go.mod h1:tso14hwzqX4VbnWTNsxiL0DvMb2OwbGISFA7jDibdWc=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.opencensus.io v0.15.0/go.mod h1:UffZAU+4sDEINUGP/B7UfBBkq4fqLu9zXAX7ke6CHW0=
Expand Down Expand Up @@ -892,6 +894,7 @@ google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQ
gopkg.in/DataDog/dd-trace-go.v1 v1.19.0/go.mod h1:DVp8HmDh8PuTu2Z0fVVlBsyWaC++fzwVCaGWylTe3tg=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d h1:TxyelI5cVkbREznMhfzycHdkp5cLA7DpE+GKjSslYhM=
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d/go.mod h1:cuepJuh7vyXfUyUwEgHQXw849cJrilpS5NeIjOWESAw=
gopkg.in/bsm/ratelimit.v1 v1.0.0-20160220154919-db14e161995a/go.mod h1:KF9sEfUPAXdG8Oev9e99iLGnl2uJMjc5B+4y3O7x610=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand All @@ -912,6 +915,7 @@ gopkg.in/jcmturner/dnsutils.v1 v1.0.1/go.mod h1:m3v+5svpVOhtFAP/wSz+yzh4Mc0Fg7eR
gopkg.in/jcmturner/goidentity.v3 v3.0.0/go.mod h1:oG2kH0IvSYNIu80dVAyu/yoefjq1mNfM5bm88whjWx4=
gopkg.in/jcmturner/gokrb5.v7 v7.2.3/go.mod h1:l8VISx+WGYp+Fp7KRbsiUuXTTOnxIc3Tuvyavf11/WM=
gopkg.in/jcmturner/rpc.v1 v1.1.0/go.mod h1:YIdkC4XfD6GXbzje11McwsDuOlZQSb9W4vfLvuNnlv8=
gopkg.in/ldap.v3 v3.1.0 h1:DIDWEjI7vQWREh0S8X5/NFPCZ3MCVd55LmXKPW4XLGE=
gopkg.in/ldap.v3 v3.1.0/go.mod h1:dQjCc0R0kfyFjIlWNMH1DORwUASZyDxo2Ry1B51dXaQ=
gopkg.in/ns1/ns1-go.v2 v2.0.0-20190730140822-b51389932cbc/go.mod h1:VV+3haRsgDiVLxyifmMBrBIuCWFBPYKbRssXB9z67Hw=
gopkg.in/olivere/elastic.v5 v5.0.82/go.mod h1:uhHoB4o3bvX5sorxBU29rPcmBQdV2Qfg0FBrx5D6pV0=
Expand Down
84 changes: 84 additions & 0 deletions pkg/server/http/server.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,97 @@
package http

import (
"encoding/json"
"net/http"
"time"

"github.com/micro/go-micro/util/log"
"github.com/micro/go-micro/web"
"github.com/owncloud/ocis-graph/pkg/config"
"github.com/owncloud/ocis-graph/pkg/flagset"
"github.com/owncloud/ocis-graph/pkg/version"
msgraph "github.com/yaegashi/msgraph.go/v1.0"
ldap "gopkg.in/ldap.v3"
)

func createUserModel(displayName string, id string) *msgraph.User {
return &msgraph.User{
DisplayName: &displayName,
GivenName: &displayName,
DirectoryObject: msgraph.DirectoryObject{
Entity: msgraph.Entity{
ID: &id,
},
},
}

}

func writeResponse(v interface{}, writer http.ResponseWriter) {
js, err := json.Marshal(v)
if err != nil {
//p.srv.Logger().Errorf("owncloud-plugin: error encoding response as json %s", err)
writer.WriteHeader(http.StatusInternalServerError)
return
}

writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(http.StatusOK)
writer.Write(js)
}

func handleMe(writer http.ResponseWriter, req *http.Request) {
me := createUserModel("Alice", "1234-5678-9000-000")
writeResponse(me, writer)
}

func handleUsers(writer http.ResponseWriter, req *http.Request) {
con, err := ldap.Dial("tcp", "localhost:10389")
if err != nil {
//p.srv.Logger().Errorf("owncloud-plugin: error encoding response as json %s", err)
writer.WriteHeader(http.StatusInternalServerError)
writer.Write([]byte("ldap dail failed"))
return
}
err = con.Bind("cn=admin,dc=example,dc=org", "admin")
if err != nil {
//p.srv.Logger().Errorf("owncloud-plugin: error encoding response as json %s", err)
writer.WriteHeader(http.StatusInternalServerError)
writer.Write([]byte("ldap bind failed"))
return
}

// Search for the given username
searchRequest := ldap.NewSearchRequest(
"ou=groups,dc=example,dc=org",
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
"(objectclass=*)",
[]string{"dn", "uuid", "uid", "givenName", "mail"},
nil,
)

sr, err := con.Search(searchRequest)
if err != nil {
//p.srv.Logger().Errorf("owncloud-plugin: error encoding response as json %s", err)
writer.WriteHeader(http.StatusInternalServerError)
writer.Write([]byte("ldap search failed: " + err.Error()))
return
}
users := make([]*msgraph.User, len(sr.Entries))
for i := 0; i < len(sr.Entries); i++ {
users[i] = createUserModel(sr.Entries[i].DN, "1234-5678-9000-000")
}
/*
users := make([]*msgraph.User, 4)
users[0] = createUserModel("Alice", "1234-5678-9000-000")
users[1] = createUserModel("Bob", "1234-5678-9000-001")
users[2] = createUserModel("Carol", "1234-5678-9000-002")
users[3] = createUserModel("Dave", "1234-5678-9000-003")
*/
// TODO: the response has to hold a root element named value ...
writeResponse(users, writer)
}

func Server(opts ...Option) (web.Service, error) {
options := newOptions(opts...)
log.Infof("Server [http] listening on [%s]", options.Config.HTTP.Addr)
Expand All @@ -35,5 +117,7 @@ func Server(opts ...Option) (web.Service, error) {
)

service.Init()
service.HandleFunc("/v1.0/me", handleMe)
service.HandleFunc("/v1.0/users", handleUsers)
return service, nil
}

0 comments on commit de28802

Please sign in to comment.