2022-04-04 00:09:04 +08:00
|
|
|
// Copyright 2022 The Casdoor Authors. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2022-04-11 21:11:31 +08:00
|
|
|
"encoding/xml"
|
2022-04-04 00:09:04 +08:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/casdoor/casdoor/object"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
InvalidRequest string = "INVALID_REQUEST"
|
|
|
|
InvalidTicketSpec string = "INVALID_TICKET_SPEC"
|
|
|
|
UnauthorizedServiceProxy string = "UNAUTHORIZED_SERVICE_PROXY"
|
|
|
|
InvalidProxyCallback string = "INVALID_PROXY_CALLBACK"
|
|
|
|
InvalidTicket string = "INVALID_TICKET"
|
|
|
|
InvalidService string = "INVALID_SERVICE"
|
2022-08-09 16:50:49 +08:00
|
|
|
InternalError string = "INTERNAL_ERROR"
|
2022-04-04 00:09:04 +08:00
|
|
|
UnauthorizedService string = "UNAUTHORIZED_SERVICE"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *RootController) CasValidate() {
|
|
|
|
ticket := c.Input().Get("ticket")
|
|
|
|
service := c.Input().Get("service")
|
|
|
|
c.Ctx.Output.Header("Content-Type", "text/html; charset=utf-8")
|
|
|
|
if service == "" || ticket == "" {
|
|
|
|
c.Ctx.Output.Body([]byte("no\n"))
|
|
|
|
return
|
|
|
|
}
|
2022-04-11 21:11:31 +08:00
|
|
|
if ok, response, issuedService, _ := object.GetCasTokenByTicket(ticket); ok {
|
2022-08-07 12:26:14 +08:00
|
|
|
// check whether service is the one for which we previously issued token
|
2022-04-04 00:09:04 +08:00
|
|
|
if issuedService == service {
|
|
|
|
c.Ctx.Output.Body([]byte(fmt.Sprintf("yes\n%s\n", response.User)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-08-07 12:26:14 +08:00
|
|
|
// token not found
|
2022-04-04 00:09:04 +08:00
|
|
|
c.Ctx.Output.Body([]byte("no\n"))
|
|
|
|
}
|
|
|
|
|
2022-04-11 21:11:31 +08:00
|
|
|
func (c *RootController) CasServiceValidate() {
|
|
|
|
ticket := c.Input().Get("ticket")
|
|
|
|
format := c.Input().Get("format")
|
|
|
|
if !strings.HasPrefix(ticket, "ST") {
|
|
|
|
c.sendCasAuthenticationResponseErr(InvalidTicket, fmt.Sprintf("Ticket %s not recognized", ticket), format)
|
|
|
|
}
|
|
|
|
c.CasP3ServiceAndProxyValidate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RootController) CasProxyValidate() {
|
|
|
|
ticket := c.Input().Get("ticket")
|
|
|
|
format := c.Input().Get("format")
|
|
|
|
if !strings.HasPrefix(ticket, "PT") {
|
|
|
|
c.sendCasAuthenticationResponseErr(InvalidTicket, fmt.Sprintf("Ticket %s not recognized", ticket), format)
|
|
|
|
}
|
|
|
|
c.CasP3ServiceAndProxyValidate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RootController) CasP3ServiceAndProxyValidate() {
|
2022-04-04 00:09:04 +08:00
|
|
|
ticket := c.Input().Get("ticket")
|
|
|
|
format := c.Input().Get("format")
|
|
|
|
service := c.Input().Get("service")
|
|
|
|
pgtUrl := c.Input().Get("pgtUrl")
|
|
|
|
|
|
|
|
serviceResponse := object.CasServiceResponse{
|
|
|
|
Xmlns: "http://www.yale.edu/tp/cas",
|
|
|
|
}
|
|
|
|
|
2022-08-07 12:26:14 +08:00
|
|
|
// check whether all required parameters are met
|
2022-04-04 00:09:04 +08:00
|
|
|
if service == "" || ticket == "" {
|
|
|
|
c.sendCasAuthenticationResponseErr(InvalidRequest, "service and ticket must exist", format)
|
|
|
|
return
|
|
|
|
}
|
2022-04-11 21:11:31 +08:00
|
|
|
ok, response, issuedService, userId := object.GetCasTokenByTicket(ticket)
|
2022-08-07 12:26:14 +08:00
|
|
|
// find the token
|
2022-04-11 21:11:31 +08:00
|
|
|
if ok {
|
2022-08-07 12:26:14 +08:00
|
|
|
// check whether service is the one for which we previously issued token
|
2022-04-04 00:09:04 +08:00
|
|
|
if strings.HasPrefix(service, issuedService) {
|
|
|
|
serviceResponse.Success = response
|
|
|
|
} else {
|
2022-08-07 12:26:14 +08:00
|
|
|
// service not match
|
2022-04-04 00:09:04 +08:00
|
|
|
c.sendCasAuthenticationResponseErr(InvalidService, fmt.Sprintf("service %s and %s does not match", service, issuedService), format)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-07 12:26:14 +08:00
|
|
|
// token not found
|
2022-04-04 00:09:04 +08:00
|
|
|
c.sendCasAuthenticationResponseErr(InvalidTicket, fmt.Sprintf("Ticket %s not recognized", ticket), format)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if pgtUrl != "" && serviceResponse.Failure == nil {
|
2022-08-07 12:26:14 +08:00
|
|
|
// that means we are in proxy web flow
|
2022-04-11 21:11:31 +08:00
|
|
|
pgt := object.StoreCasTokenForPgt(serviceResponse.Success, service, userId)
|
2022-04-04 00:09:04 +08:00
|
|
|
pgtiou := serviceResponse.Success.ProxyGrantingTicket
|
2022-08-07 12:26:14 +08:00
|
|
|
// todo: check whether it is https
|
2022-04-04 00:09:04 +08:00
|
|
|
pgtUrlObj, err := url.Parse(pgtUrl)
|
|
|
|
if pgtUrlObj.Scheme != "https" {
|
|
|
|
c.sendCasAuthenticationResponseErr(InvalidProxyCallback, "callback is not https", format)
|
|
|
|
return
|
|
|
|
}
|
2022-08-07 12:26:14 +08:00
|
|
|
// make a request to pgturl passing pgt and pgtiou
|
2022-04-04 00:09:04 +08:00
|
|
|
if err != nil {
|
2022-08-09 16:50:49 +08:00
|
|
|
c.sendCasAuthenticationResponseErr(InternalError, err.Error(), format)
|
2022-04-04 00:09:04 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
param := pgtUrlObj.Query()
|
|
|
|
param.Add("pgtId", pgt)
|
|
|
|
param.Add("pgtIou", pgtiou)
|
|
|
|
pgtUrlObj.RawQuery = param.Encode()
|
|
|
|
|
|
|
|
request, err := http.NewRequest("GET", pgtUrlObj.String(), nil)
|
|
|
|
if err != nil {
|
2022-08-09 16:50:49 +08:00
|
|
|
c.sendCasAuthenticationResponseErr(InternalError, err.Error(), format)
|
2022-04-04 00:09:04 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(request)
|
|
|
|
if err != nil || !(resp.StatusCode >= 200 && resp.StatusCode < 400) {
|
2022-08-07 12:26:14 +08:00
|
|
|
// failed to send request
|
2022-04-04 00:09:04 +08:00
|
|
|
c.sendCasAuthenticationResponseErr(InvalidProxyCallback, err.Error(), format)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// everything is ok, send the response
|
|
|
|
if format == "json" {
|
|
|
|
c.Data["json"] = serviceResponse
|
|
|
|
c.ServeJSON()
|
|
|
|
} else {
|
|
|
|
c.Data["xml"] = serviceResponse
|
|
|
|
c.ServeXML()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RootController) CasProxy() {
|
|
|
|
pgt := c.Input().Get("pgt")
|
|
|
|
targetService := c.Input().Get("targetService")
|
|
|
|
format := c.Input().Get("format")
|
|
|
|
if pgt == "" || targetService == "" {
|
|
|
|
c.sendCasProxyResponseErr(InvalidRequest, "pgt and targetService must exist", format)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-11 21:11:31 +08:00
|
|
|
ok, authenticationSuccess, issuedService, userId := object.GetCasTokenByPgt(pgt)
|
2022-04-04 00:09:04 +08:00
|
|
|
if !ok {
|
|
|
|
c.sendCasProxyResponseErr(UnauthorizedService, "service not authorized", format)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newAuthenticationSuccess := authenticationSuccess.DeepCopy()
|
|
|
|
if newAuthenticationSuccess.Proxies == nil {
|
|
|
|
newAuthenticationSuccess.Proxies = &object.CasProxies{}
|
|
|
|
}
|
|
|
|
newAuthenticationSuccess.Proxies.Proxies = append(newAuthenticationSuccess.Proxies.Proxies, issuedService)
|
2022-04-11 21:11:31 +08:00
|
|
|
proxyTicket := object.StoreCasTokenForProxyTicket(&newAuthenticationSuccess, targetService, userId)
|
2022-04-04 00:09:04 +08:00
|
|
|
|
|
|
|
serviceResponse := object.CasServiceResponse{
|
|
|
|
Xmlns: "http://www.yale.edu/tp/cas",
|
|
|
|
ProxySuccess: &object.CasProxySuccess{
|
|
|
|
ProxyTicket: proxyTicket,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if format == "json" {
|
|
|
|
c.Data["json"] = serviceResponse
|
|
|
|
c.ServeJSON()
|
|
|
|
} else {
|
|
|
|
c.Data["xml"] = serviceResponse
|
|
|
|
c.ServeXML()
|
|
|
|
}
|
|
|
|
}
|
2022-04-11 21:11:31 +08:00
|
|
|
|
|
|
|
func (c *RootController) SamlValidate() {
|
|
|
|
c.Ctx.Output.Header("Content-Type", "text/xml; charset=utf-8")
|
|
|
|
target := c.Input().Get("TARGET")
|
|
|
|
body := c.Ctx.Input.RequestBody
|
|
|
|
envelopRequest := struct {
|
|
|
|
XMLName xml.Name `xml:"Envelope"`
|
|
|
|
Body struct {
|
|
|
|
XMLName xml.Name `xml:"Body"`
|
|
|
|
Content string `xml:",innerxml"`
|
|
|
|
}
|
|
|
|
}{}
|
|
|
|
|
|
|
|
err := xml.Unmarshal(body, &envelopRequest)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response, service, err := object.GetValidationBySaml(envelopRequest.Body.Content, c.Ctx.Request.Host)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(target, service) {
|
2022-10-23 15:16:24 +08:00
|
|
|
c.ResponseError(fmt.Sprintf(c.T("CasErr.ServiceDoNotMatch"), target, service))
|
2022-04-11 21:11:31 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
envelopResponse := struct {
|
2022-04-11 21:11:31 +08:00
|
|
|
XMLName xml.Name `xml:"SOAP-ENV:Envelope"`
|
|
|
|
Xmlns string `xml:"xmlns:SOAP-ENV"`
|
|
|
|
Body struct {
|
|
|
|
XMLName xml.Name `xml:"SOAP-ENV:Body"`
|
|
|
|
Content string `xml:",innerxml"`
|
|
|
|
}
|
|
|
|
}{}
|
2022-08-09 16:50:49 +08:00
|
|
|
envelopResponse.Xmlns = "http://schemas.xmlsoap.org/soap/envelope/"
|
|
|
|
envelopResponse.Body.Content = response
|
2022-04-11 21:11:31 +08:00
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
data, err := xml.Marshal(envelopResponse)
|
2022-04-11 21:11:31 +08:00
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2022-08-09 16:50:49 +08:00
|
|
|
c.Ctx.Output.Body(data)
|
2022-04-11 21:11:31 +08:00
|
|
|
}
|
|
|
|
|
2022-04-04 00:09:04 +08:00
|
|
|
func (c *RootController) sendCasProxyResponseErr(code, msg, format string) {
|
|
|
|
serviceResponse := object.CasServiceResponse{
|
|
|
|
Xmlns: "http://www.yale.edu/tp/cas",
|
|
|
|
ProxyFailure: &object.CasProxyFailure{
|
|
|
|
Code: code,
|
|
|
|
Message: msg,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if format == "json" {
|
|
|
|
c.Data["json"] = serviceResponse
|
|
|
|
c.ServeJSON()
|
|
|
|
} else {
|
|
|
|
c.Data["xml"] = serviceResponse
|
|
|
|
c.ServeXML()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RootController) sendCasAuthenticationResponseErr(code, msg, format string) {
|
|
|
|
serviceResponse := object.CasServiceResponse{
|
|
|
|
Xmlns: "http://www.yale.edu/tp/cas",
|
|
|
|
Failure: &object.CasAuthenticationFailure{
|
|
|
|
Code: code,
|
|
|
|
Message: msg,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if format == "json" {
|
|
|
|
c.Data["json"] = serviceResponse
|
|
|
|
c.ServeJSON()
|
|
|
|
} else {
|
|
|
|
c.Data["xml"] = serviceResponse
|
|
|
|
c.ServeXML()
|
|
|
|
}
|
|
|
|
}
|