コード例 #1
0
ファイル: api.go プロジェクト: surma/importalias
func (api *APIv1) ClaimDomain(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	uid := context.Get(r, "uid").(*gouuid.UUID)
	domain := dns.Fqdn(vars["domain"])
	m := new(dns.Msg)
	m.SetQuestion(domain, dns.TypeCNAME)
	c := new(dns.Client)
	rr, _, err := c.Exchange(m, "8.8.8.8:53")
	if err != nil {
		log.Printf("Could not check domain’s records: %s", err)
		http.Error(w, "Could not check domain’s records", http.StatusInternalServerError)
		return
	}

	if !containsValidCNAMERecord(uid, rr.Answer) {
		http.Error(w, "Did not find your UID in domain’s CNAME records", http.StatusUnauthorized)
		return
	}

	err = api.domainmgr.ClaimDomain(vars["domain"], uid)
	if err == ErrAlreadyClaimed {
		log.Printf("Domain %s alread claimed", vars["domain"])
		http.Error(w, "Domain already claimed", http.StatusForbidden)
		return
	} else if err != nil {
		log.Printf("Could not claim domain %s: %s", vars["domain"], err)
		http.Error(w, "Could not claim domain", http.StatusInternalServerError)
		return
	}
	http.Error(w, "", http.StatusNoContent)
}
コード例 #2
0
ファイル: auth.go プロジェクト: surma/importalias
func LogoutHandler(w http.ResponseWriter, r *http.Request) {
	session := context.Get(r, "session").(*sessions.Session)
	session.Values = make(map[interface{}]interface{})
	session.Save(r, w)

	CALLBACK_TEMPLATE.Execute(w, nil)
}
コード例 #3
0
ファイル: api.go プロジェクト: surma/importalias
func (api *APIv1) DeleteDomain(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	uid := context.Get(r, "uid").(*gouuid.UUID)
	err := api.domainmgr.DeleteDomain(vars["domain"], uid)
	if err != nil {
		log.Printf("Could not delete domain %s: %s", vars["domain"], err)
		http.Error(w, "Could not delete domain", http.StatusInternalServerError)
		return
	}
	http.Error(w, "", http.StatusNoContent)
}
コード例 #4
0
ファイル: api.go プロジェクト: surma/importalias
func (api *APIv1) ListAliases(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	uid := context.Get(r, "uid").(*gouuid.UUID)

	domain, err := api.domainmgr.FindDomain(vars["domain"])
	if err != nil || !domain.IsOwnedBy(uid) {
		log.Printf("Could not find domain %s: %s", vars["domain"], err)
		http.Error(w, "Could not find domain", http.StatusNotFound)
		return
	}
	enc := json.NewEncoder(w)
	enc.Encode(domain.Aliases)
}
コード例 #5
0
ファイル: auth.go プロジェクト: surma/importalias
func ValidateUID(umgr UserManager) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		uid, ok := context.Get(r, "uid").(*gouuid.UUID)
		if !ok {
			http.NotFound(w, r)
			return
		}
		_, err := umgr.FindByUID(uid)
		if err != nil {
			http.NotFound(w, r)
			return
		}
	})
}
コード例 #6
0
ファイル: api.go プロジェクト: surma/importalias
func (api *APIv1) GetDomains(w http.ResponseWriter, r *http.Request) {
	uid := context.Get(r, "uid").(*gouuid.UUID)
	domains, err := api.domainmgr.DomainsByOwner(uid)
	if err != nil {
		log.Printf("Could not list domains of %s: %s", uid, err)
		http.Error(w, "Could not list domains", http.StatusInternalServerError)
		return
	}
	domainnames := make([]string, len(domains))
	for i, domain := range domains {
		domainnames[i] = domain.Name
	}

	enc := json.NewEncoder(w)
	enc.Encode(domainnames)
}
コード例 #7
0
ファイル: api.go プロジェクト: surma/importalias
func (api *APIv1) DeleteAlias(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	uid := context.Get(r, "uid").(*gouuid.UUID)

	aid, err := gouuid.ParseString(vars["aid"])
	if err != nil {
		log.Printf("Invalid id: %s", vars["aid"])
		http.Error(w, "Invalid id format", http.StatusNotFound)
		return
	}

	err = api.domainmgr.DeleteAlias(&aid, uid)
	if err != nil {
		log.Printf("Could not delete alias %s: %s", vars["aid"], err)
		http.Error(w, "Could not delete alias", http.StatusNotFound)
		return
	}
	http.Error(w, "", http.StatusNoContent)
}
コード例 #8
0
ファイル: api.go プロジェクト: surma/importalias
func NewAPIv1(domainmgr DomainManager, usermgr UserManager) *APIv1 {
	api := &APIv1{
		domainmgr: domainmgr,
		usermgr:   usermgr,
	}

	api.Router = mux.NewRouter()
	api.Router.Path("/me").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		uid := context.Get(r, "uid").(*gouuid.UUID)
		user, err := usermgr.FindByUID(uid)
		if err != nil {
			log.Printf("Session UID invalid: %s", err)
			http.Error(w, "Could not find user", http.StatusInternalServerError)
			return
		}
		enc := json.NewEncoder(w)
		enc.Encode(user)
	})

	api.Router.Path("/domains").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		api.GetDomains(w, r)
	})
	api.Router.Path("/domains/{domain}").Methods("POST").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		api.ClaimDomain(w, r)
	})
	api.Router.Path("/domains/{domain}").Methods("GET").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		api.ListAliases(w, r)
	})
	api.Router.Path("/domains/{domain}").Methods("DELETE").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		api.DeleteDomain(w, r)
	})
	api.Router.Path("/domains/{domain}").Methods("PUT").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		api.SetAlias(w, r)
	})
	api.Router.Path("/domains/{domain}/{aid}").Methods("DELETE").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		api.DeleteAlias(w, r)
	})

	return api
}
コード例 #9
0
ファイル: api.go プロジェクト: surma/importalias
func (api *APIv1) SetAlias(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	uid := context.Get(r, "uid").(*gouuid.UUID)

	alias := &Alias{}
	dec := json.NewDecoder(r.Body)
	err := dec.Decode(alias)
	if err != nil {
		log.Printf("Invalid alias object: %s", err)
		http.Error(w, "Invalid alias object", http.StatusBadRequest)
		return
	}

	err = api.domainmgr.SetAlias(vars["domain"], alias, uid)
	if err != nil {
		log.Printf("Could not add alias to %s: %s", vars["domain"], err)
		http.Error(w, "Could not add alias", http.StatusNotFound)
		return
	}
	w.WriteHeader(http.StatusCreated)
	enc := json.NewEncoder(w)
	enc.Encode(alias.ID)
}
コード例 #10
0
ファイル: auth.go プロジェクト: surma/importalias
func (a *OAuthAuthenticator) authCallbackHandler(w http.ResponseWriter, r *http.Request) {
	transport := (&oauth.Transport{Config: a.config})
	_, err := transport.Exchange(r.FormValue("code"))
	if err != nil {
		log.Printf("%s: Could not get access token: %s", a.authname, err)
		http.Error(w, "Could not get access token", http.StatusServiceUnavailable)
		return
	}

	id, err := a.extractor.Extract(transport.Client())
	if err != nil {
		log.Printf("%s: Could not get user id: %s", a.authname, err)
		http.Error(w, "Could not get user id", http.StatusServiceUnavailable)
		return
	}

	// Login
	user, err := a.usermgr.FindByAuthenticator(a.authname, id)
	if err != nil && err != ErrUserNotFound {
		log.Printf("Could not query user database: %s", err)
		http.Error(w, "Could not query user database", http.StatusInternalServerError)
		return
	} else if err == ErrUserNotFound {
		// New user
		user, err = a.usermgr.New(a.authname, id)
		if err != nil {
			log.Printf("Error creating user: %s", err)
			http.Error(w, "Error creating user", http.StatusInternalServerError)
			return
		}
	}
	session := context.Get(r, "session").(*sessions.Session)
	session.Values["uid"] = user.UID
	session.Save(r, w)
	CALLBACK_TEMPLATE.Execute(w, nil)
}