webfinger.go 1.95 KB
Newer Older
zauberstuhl's avatar
zauberstuhl committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
package federation
//
// GangGo Diaspora Federation Library
// Copyright (C) 2017 Lukas Matt <lukas@zauberstuhl.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

import (
zauberstuhl's avatar
zauberstuhl committed
21
  "github.com/Zauberstuhl/go-xml"
22
  "fmt"
zauberstuhl's avatar
zauberstuhl committed
23 24 25 26 27 28
  "errors"
)

type WebFinger struct {
  Host string
  Handle string
29
  Data WebfingerData
zauberstuhl's avatar
zauberstuhl committed
30 31
}

32 33 34 35 36 37 38
type WebfingerData struct {
  // xml
  XMLName xml.Name `xml:"XRD" json:"-"`
  Xmlns string `xml:"xmlns,attr" json:"-"`
  Alias string `xml:"Alias,omitempty" json:"-"`
  // json
  Aliases []string `json:"aliases" xml:"-"`
zauberstuhl's avatar
zauberstuhl committed
39

40 41
  Subject string `json:"subject" xml:"Subject,omitempty"`
  Links []WebfingerDataLink `json:"links" xml:"Link"`
zauberstuhl's avatar
zauberstuhl committed
42 43
}

44 45 46
type WebfingerDataLink struct {
  // xml
  XMLName xml.Name `xml:"Link" json:"-"`
47

48 49 50 51
  Rel string `json:"rel" xml:"rel,attr"`
  Type string `json:"type,omitempty" xml:"type,attr"`
  Href string `json:"href,omitempty" xml:"href,attr,omitempty"`
  Template string `json:"template,omitempty" xml:"template,attr,omitempty"`
52 53
}

zauberstuhl's avatar
zauberstuhl committed
54
func (w *WebFinger) Discovery() error {
55
  url := fmt.Sprintf("%s/.well-known/webfinger?resource=acct:%s", w.Host, w.Handle)
56
  err := FetchJson("GET", url, nil, &w.Data)
zauberstuhl's avatar
zauberstuhl committed
57
  if err != nil {
58 59 60 61 62
    url = fmt.Sprintf("%s/webfinger?q=acct:%s", w.Host, w.Handle)
    err = FetchXml("GET", url, nil, &w.Data)
    if err != nil {
      return err
    }
zauberstuhl's avatar
zauberstuhl committed
63
  }
zauberstuhl's avatar
zauberstuhl committed
64

65
  if len(w.Data.Links) < 1 {
66
    return errors.New("Webfinger Links missing")
zauberstuhl's avatar
zauberstuhl committed
67
  }
68
  return nil
zauberstuhl's avatar
zauberstuhl committed
69
}