APExchange.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package service
  2. import (
  3. "time"
  4. "gopkg.in/jcmturner/gokrb5.v7/credentials"
  5. "gopkg.in/jcmturner/gokrb5.v7/iana/errorcode"
  6. "gopkg.in/jcmturner/gokrb5.v7/messages"
  7. )
  8. // VerifyAPREQ verifies an AP_REQ sent to the service. Returns a boolean for if the AP_REQ is valid and the client's principal name and realm.
  9. func VerifyAPREQ(APReq messages.APReq, s *Settings) (bool, *credentials.Credentials, error) {
  10. var creds *credentials.Credentials
  11. ok, err := APReq.Verify(s.Keytab, s.MaxClockSkew(), s.ClientAddress())
  12. if err != nil || !ok {
  13. return false, creds, err
  14. }
  15. if s.RequireHostAddr() && len(APReq.Ticket.DecryptedEncPart.CAddr) < 1 {
  16. return false, creds,
  17. messages.NewKRBError(APReq.Ticket.SName, APReq.Ticket.Realm, errorcode.KRB_AP_ERR_BADADDR, "ticket does not contain HostAddress values required")
  18. }
  19. // Check for replay
  20. rc := GetReplayCache(s.MaxClockSkew())
  21. if rc.IsReplay(APReq.Ticket.SName, APReq.Authenticator) {
  22. return false, creds,
  23. messages.NewKRBError(APReq.Ticket.SName, APReq.Ticket.Realm, errorcode.KRB_AP_ERR_REPEAT, "replay detected")
  24. }
  25. c := credentials.NewFromPrincipalName(APReq.Authenticator.CName, APReq.Authenticator.CRealm)
  26. creds = c
  27. creds.SetAuthTime(time.Now().UTC())
  28. creds.SetAuthenticated(true)
  29. creds.SetValidUntil(APReq.Ticket.DecryptedEncPart.EndTime)
  30. //PAC decoding
  31. if !s.disablePACDecoding {
  32. isPAC, pac, err := APReq.Ticket.GetPACType(s.Keytab, s.KeytabPrincipal(), s.Logger())
  33. if isPAC && err != nil {
  34. return false, creds, err
  35. }
  36. if isPAC {
  37. // There is a valid PAC. Adding attributes to creds
  38. creds.SetADCredentials(credentials.ADCredentials{
  39. GroupMembershipSIDs: pac.KerbValidationInfo.GetGroupMembershipSIDs(),
  40. LogOnTime: pac.KerbValidationInfo.LogOnTime.Time(),
  41. LogOffTime: pac.KerbValidationInfo.LogOffTime.Time(),
  42. PasswordLastSet: pac.KerbValidationInfo.PasswordLastSet.Time(),
  43. EffectiveName: pac.KerbValidationInfo.EffectiveName.Value,
  44. FullName: pac.KerbValidationInfo.FullName.Value,
  45. UserID: int(pac.KerbValidationInfo.UserID),
  46. PrimaryGroupID: int(pac.KerbValidationInfo.PrimaryGroupID),
  47. LogonServer: pac.KerbValidationInfo.LogonServer.Value,
  48. LogonDomainName: pac.KerbValidationInfo.LogonDomainName.Value,
  49. LogonDomainID: pac.KerbValidationInfo.LogonDomainID.String(),
  50. })
  51. }
  52. }
  53. return true, creds, nil
  54. }