mirror of
https://github.com/inverse-inc/sogo.git
synced 2026-11-22 15:14:21 +00:00
feat(password-recovery): Add password recovery with secret question or secondary email
This commit is contained in:
1 parent
6931186bba
commit
656807b77d
47 files changed
+1729
-159
No files matched your search
@@ -72,6 +72,7 @@ SOGo_HEADER_FILES = \
|
||||
SOGoDAVAuthenticator.h \
|
||||
SOGoProxyAuthenticator.h \
|
||||
SOGoStaticAuthenticator.h \
|
||||
SOGoEmptyAuthenticator.h \
|
||||
SOGoWebAuthenticator.h \
|
||||
SOGoWebDAVAclManager.h \
|
||||
SOGoWebDAVValue.h \
|
||||
@@ -88,7 +89,9 @@ SOGo_HEADER_FILES = \
|
||||
\
|
||||
SOGoCredentialsFile.h \
|
||||
SOGoTextTemplateFile.h \
|
||||
SOGoZipArchiver.h
|
||||
SOGoZipArchiver.h \
|
||||
\
|
||||
JWT.h
|
||||
|
||||
all::
|
||||
@touch SOGoBuild.m
|
||||
@@ -154,6 +157,7 @@ SOGo_OBJC_FILES = \
|
||||
SOGoDAVAuthenticator.m \
|
||||
SOGoProxyAuthenticator.m \
|
||||
SOGoStaticAuthenticator.m \
|
||||
SOGoEmptyAuthenticator.m \
|
||||
SOGoWebAuthenticator.m \
|
||||
SOGoWebDAVAclManager.m \
|
||||
SOGoWebDAVValue.m \
|
||||
@@ -170,7 +174,9 @@ SOGo_OBJC_FILES = \
|
||||
\
|
||||
SOGoCredentialsFile.m \
|
||||
SOGoTextTemplateFile.m \
|
||||
SOGoZipArchiver.m
|
||||
SOGoZipArchiver.m \
|
||||
\
|
||||
JWT.m
|
||||
|
||||
SOGo_C_FILES += lmhash.c aes.c crypt_blowfish.c pkcs5_pbkdf2.c
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/* JWT.h - this file is part of SOGo
|
||||
*
|
||||
* Copyright (C) 2022 Alinto
|
||||
*
|
||||
* This file 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 2, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This file 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; see the file COPYING. If not, write to
|
||||
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#include <Foundation/NSString.h>
|
||||
|
||||
#ifndef JWT_H
|
||||
#define JWT_H
|
||||
|
||||
|
||||
@interface JWT : NSObject
|
||||
{
|
||||
@private
|
||||
NSString *JWTSecret;
|
||||
}
|
||||
|
||||
+ (JWT *)sharedInstance;
|
||||
- (NSString *) getJWTWithData: (NSDictionary *) data andValidity: (int)validitySec;
|
||||
- (NSDictionary *) getDataWithJWT: (NSString *) JWTToken andValidity: (BOOL *)isValid isExpired: (BOOL *)isExpired;
|
||||
|
||||
@end
|
||||
|
||||
#endif /* JWT_H */
|
||||
@@ -0,0 +1,268 @@
|
||||
/* JWT.m - this file is part of SOGo
|
||||
*
|
||||
* Copyright (C) 2022 Alinto
|
||||
*
|
||||
* This file is part of SOGo.
|
||||
*
|
||||
* This file 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 2, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This file 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; see the file COPYING. If not, write to
|
||||
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#import "JWT.h"
|
||||
#import <Foundation/NSDictionary.h>
|
||||
#import <Foundation/NSData.h>
|
||||
#import <GNUstepBase/GSMime.h>
|
||||
#import <SOGo/SOGoSystemDefaults.h>
|
||||
#import <SOGo/NSDictionary+Utilities.h>
|
||||
#import <SOGo/NSString+Utilities.h>
|
||||
#include <openssl/hmac.h>
|
||||
#include <openssl/evp.h>
|
||||
|
||||
#define HS256_TOKEN_LENGH 43
|
||||
|
||||
static const NSString *kExpKey = @"exp";
|
||||
static const NSString *kAlgKey = @"alg";
|
||||
static const NSString *kTypKey = @"typ";
|
||||
static const NSString *kAlg = @"HS256";
|
||||
static const NSString *kTyp = @"JWT";
|
||||
|
||||
@implementation JWT
|
||||
|
||||
- (id) init
|
||||
{
|
||||
if ((self = [super init]))
|
||||
{
|
||||
self->JWTSecret = [[SOGoSystemDefaults sharedSystemDefaults] JWTSecret];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void) dealloc
|
||||
{
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
+ (JWT *)sharedInstance
|
||||
{
|
||||
static JWT *sharedInstance = nil;
|
||||
|
||||
if (!sharedInstance)
|
||||
{
|
||||
sharedInstance = [[self alloc] init];
|
||||
[sharedInstance retain];
|
||||
}
|
||||
|
||||
return sharedInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode base64 data
|
||||
* @param data The input data
|
||||
* @param length The input data length
|
||||
* @return Data encoded in base64
|
||||
*/
|
||||
- (NSString *) base64EncodeWithData: (NSData *)data length: (NSUInteger)length {
|
||||
NSData *dataBase64;
|
||||
dataBase64 = [GSMimeDocument encodeBase64: data];
|
||||
return [[
|
||||
[[NSString stringWithCString: [dataBase64 bytes] length: length]
|
||||
stringByReplacingOccurrencesOfString:@"+" withString:@"-"]
|
||||
stringByReplacingOccurrencesOfString:@"/" withString:@"_"]
|
||||
stringByReplacingOccurrencesOfString:@"=" withString:@""];
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode base64 string
|
||||
* @param data The input string
|
||||
* @param length The input data length
|
||||
* @return Data encoded in base64
|
||||
*/
|
||||
- (NSString *) base64EncodeWithString: (NSString *)data {
|
||||
return [[
|
||||
[[GSMimeDocument encodeBase64String: data]
|
||||
stringByReplacingOccurrencesOfString:@"+" withString:@"-"]
|
||||
stringByReplacingOccurrencesOfString:@"/" withString:@"_"]
|
||||
stringByReplacingOccurrencesOfString:@"=" withString:@""];
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode base64 string
|
||||
* @param data The base64 input string
|
||||
* @return Decoded data
|
||||
*/
|
||||
- (NSDictionary *) base64DecodeWithString: (NSString *)data {
|
||||
NSString *decodedData;
|
||||
NSDictionary *output;
|
||||
|
||||
output = nil;
|
||||
decodedData = [GSMimeDocument decodeBase64String: data];
|
||||
if ([decodedData isJSONString]) {
|
||||
output = (NSDictionary *)[decodedData objectFromJSONString];
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate JWT Token encoded with HS256 algorithm
|
||||
* @param dict The payload content
|
||||
* @return A valid JWT token (header + payload + signature)
|
||||
*/
|
||||
- (NSString *) getHS256TokenForData: (NSDictionary *)dict withSecret: (NSString *)secret {
|
||||
unsigned char hs256[HS256_TOKEN_LENGH] = {};
|
||||
NSString *headerBase64, *payloadBase64, *content, *token;
|
||||
NSArray *sortedKeys;
|
||||
NSMutableDictionary *sortedDict
|
||||
|
||||
// Reorder dictionary keys
|
||||
sortedKeys = [[dict allKeys] sortedArrayUsingSelector: @selector(compare:)];
|
||||
sortedDict = [NSMutableDictionary dictionary];
|
||||
for (NSString *key in sortedKeys)
|
||||
[sortedDict setObject:[dict objectForKey: key] forKey: key];
|
||||
|
||||
headerBase64 = [self base64EncodeWithString:
|
||||
[[NSDictionary dictionaryWithObjectsAndKeys:kAlg, kAlgKey, kTyp, kTypKey, nil] jsonRepresentation]];
|
||||
payloadBase64 = [self base64EncodeWithString: [sortedDict jsonRepresentation]];
|
||||
content = [NSString stringWithFormat: @"%@.%@", headerBase64, payloadBase64, nil];
|
||||
|
||||
HMAC(EVP_sha256(),
|
||||
[secret UTF8String], [secret length],
|
||||
[content UTF8String], [content length],
|
||||
hs256, NULL);
|
||||
|
||||
token = [self base64EncodeWithData: [NSData dataWithBytes:hs256 length: HS256_TOKEN_LENGH] length: HS256_TOKEN_LENGH];
|
||||
|
||||
return [NSString stringWithFormat: @"%@.%@", content, token, nil];
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate JWT Token encoded with HS256 algorithm
|
||||
* @param data The payload content
|
||||
* @param validitySec Validity duration, in seconds
|
||||
* @return A valid JWT token (header + payload + signature)
|
||||
*/
|
||||
- (NSString *)getJWTWithData: (NSDictionary *)data andValidity: (int)validitySec {
|
||||
NSMutableDictionary *dict;
|
||||
dict = [NSMutableDictionary dictionaryWithDictionary: data];
|
||||
[dict setObject:[NSString stringWithFormat:@"%.0f", ([[NSDate date] timeIntervalSince1970] + validitySec)] forKey: kExpKey];
|
||||
|
||||
return [self getHS256TokenForData: dict withSecret: self->JWTSecret];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get JWT token data and check validity token
|
||||
* @param JWTToken A JWT complete token (header + payload + signature)
|
||||
* @param isValid Reference parameter - NO if token is invalid
|
||||
* @param isExpired Reference parameter - YES if token is expired
|
||||
* @return Payload content
|
||||
*/
|
||||
- (NSDictionary *)getDataWithJWT: (NSString *)JWTToken andValidity: (BOOL *)isValid isExpired: (BOOL *)isExpired {
|
||||
NSArray *components, *reencodedComponents;
|
||||
NSString *header, *payload, *reencodedJWTToken, *signature, *reencodedSignature;
|
||||
NSDictionary *headerDict, *payloadDict;
|
||||
NSTimeInterval tokenTime;
|
||||
NSMutableDictionary *result;
|
||||
|
||||
*isValid = YES;
|
||||
*isExpired = NO;
|
||||
result = nil;
|
||||
components = [JWTToken componentsSeparatedByString:@"."];
|
||||
|
||||
if (3 != [components count]) {
|
||||
// Invalid number of components
|
||||
*isValid = NO;
|
||||
return result;
|
||||
}
|
||||
|
||||
// Check header
|
||||
///////////////
|
||||
header = (NSString *)[components objectAtIndex: 0];
|
||||
if (!header) {
|
||||
// No header
|
||||
*isValid = NO;
|
||||
return result;
|
||||
}
|
||||
headerDict = [self base64DecodeWithString: header];
|
||||
if (!headerDict) {
|
||||
// No header
|
||||
*isValid = NO;
|
||||
return result;
|
||||
}
|
||||
if (![headerDict objectForKey: kTypKey] || ![[headerDict objectForKey: kTypKey] isEqualToString: kTyp]) {
|
||||
// Invalid type
|
||||
*isValid = NO;
|
||||
return result;
|
||||
}
|
||||
if (![headerDict objectForKey: kAlgKey] || ![[headerDict objectForKey: kAlgKey] isEqualToString: kAlg]) {
|
||||
// Invalid algorithm
|
||||
*isValid = NO;
|
||||
return result;
|
||||
}
|
||||
|
||||
// Check payload
|
||||
///////////////
|
||||
payload = (NSString *)[components objectAtIndex: 1];
|
||||
if (!payload) {
|
||||
// No payload
|
||||
*isValid = NO;
|
||||
return result;
|
||||
}
|
||||
payloadDict = [self base64DecodeWithString: payload];
|
||||
if (!payloadDict) {
|
||||
// No payload
|
||||
*isValid = NO;
|
||||
return result;
|
||||
}
|
||||
if (![payloadDict objectForKey: kExpKey]) {
|
||||
// No expiration token
|
||||
*isValid = NO;
|
||||
return result;
|
||||
}
|
||||
// Check expiration
|
||||
tokenTime = [[payloadDict objectForKey: kExpKey] doubleValue];
|
||||
if (0 != tokenTime) { // 0 for infinity validation
|
||||
if ([[NSDate date] timeIntervalSince1970] > tokenTime) {
|
||||
// Token expired
|
||||
*isValid = NO;
|
||||
*isExpired = YES;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// Check signature
|
||||
///////////////
|
||||
reencodedJWTToken = [self getHS256TokenForData: payloadDict withSecret: self->JWTSecret];
|
||||
reencodedComponents = [reencodedJWTToken componentsSeparatedByString:@"."];
|
||||
if (3 != [reencodedComponents count]) {
|
||||
// Invalid number of reencoded components
|
||||
*isValid = NO;
|
||||
return result;
|
||||
}
|
||||
signature = (NSString *)[components objectAtIndex: 2];
|
||||
reencodedSignature = (NSString *)[reencodedComponents objectAtIndex: 2];
|
||||
if (![signature isEqualToString: reencodedSignature]) {
|
||||
// Invalid signature
|
||||
*isValid = NO;
|
||||
return result;
|
||||
}
|
||||
|
||||
// All is OK !
|
||||
result = [NSMutableDictionary dictionaryWithDictionary: payloadDict];
|
||||
[result removeObjectForKey: kExpKey];
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -708,14 +708,20 @@ groupObjectClasses: (NSArray *) newGroupObjectClasses
|
||||
return didChange;
|
||||
}
|
||||
|
||||
//
|
||||
//
|
||||
//
|
||||
/**
|
||||
* Change a user's password.
|
||||
* @param login the user's login name.
|
||||
* @param oldPassword the previous password.
|
||||
* @param newPassword the new password.
|
||||
* @param perr will be set if the new password is not conform to the policy.
|
||||
* @param passwordRecovery YES of this is password recovery, NO otherwise. If password recovery is set, old password won't be checked
|
||||
* @return YES if the password was successfully changed.
|
||||
*/
|
||||
- (BOOL) changePasswordForLogin: (NSString *) login
|
||||
oldPassword: (NSString *) oldPassword
|
||||
newPassword: (NSString *) newPassword
|
||||
passwordRecovery: (BOOL) passwordRecovery
|
||||
perr: (SOGoPasswordPolicyError *) perr
|
||||
|
||||
{
|
||||
NGLdapConnection *bindConnection;
|
||||
NSString *userDN;
|
||||
|
||||
@@ -58,12 +58,12 @@
|
||||
while ((currentKey = [keys nextObject]))
|
||||
{
|
||||
currentValue = [[self objectForKey: currentKey] jsonRepresentation];
|
||||
currentPair = [NSString stringWithFormat: @"%@: %@",
|
||||
currentPair = [NSString stringWithFormat: @"%@:%@",
|
||||
[currentKey jsonRepresentation], currentValue];
|
||||
[values addObject: currentPair];
|
||||
}
|
||||
representation = [NSString stringWithFormat: @"{%@}",
|
||||
[values componentsJoinedByString: @", "]];
|
||||
[values componentsJoinedByString: @","]];
|
||||
|
||||
return representation;
|
||||
}
|
||||
|
||||
@@ -36,6 +36,8 @@ typedef enum
|
||||
PolicyPasswordTooShort = 6,
|
||||
PolicyPasswordTooYoung = 7,
|
||||
PolicyPasswordInHistory = 8,
|
||||
PolicyPasswordRecoveryFailed = 9,
|
||||
PolicyPasswordRecoveryInvalidToken = 10,
|
||||
PolicyNoError = 65535,
|
||||
} SOGoPasswordPolicyError;
|
||||
|
||||
|
||||
@@ -195,4 +195,7 @@
|
||||
};
|
||||
|
||||
SOGoSubscriptionFolderFormat = "%{FolderName} (%{UserName} <%{Email}>)";
|
||||
|
||||
SOGoPasswordRecoveryMode = "Disabled";
|
||||
SOGoPasswordRecoveryQuestion = "SecretQuestion1";
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/* SOGoEmptyAuthenticator.h - this file is part of SOGo
|
||||
*
|
||||
* Copyright (C) 2022 Alinto
|
||||
*
|
||||
* This file 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 2, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This file 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; see the file COPYING. If not, write to
|
||||
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#ifndef SOGOEMPTYAUTHENTICATOR_H
|
||||
#define SOGOEMPTYAUTHENTICATOR_H
|
||||
|
||||
|
||||
|
||||
#import "SOGoAuthenticator.h"
|
||||
|
||||
/*
|
||||
SOGoEmptyAuthenticator
|
||||
This class can be used to send message without SMTP authentication.
|
||||
This is used to send technical message to users, e.g. for password recovery
|
||||
|
||||
*/
|
||||
|
||||
@interface SOGoEmptyAuthenticator : NSObject <SOGoAuthenticator>
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
+ (id) sharedSOGoEmptyAuthenticator;
|
||||
|
||||
@end
|
||||
|
||||
#endif /* SOGOEMPTYAUTHENTICATOR_H */
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
/* SOGoEmptyAuthenticator.m - this file is part of SOGo
|
||||
*
|
||||
* Copyright (C) 2022 Alinto
|
||||
*
|
||||
* This file 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 2, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This file 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; see the file COPYING. If not, write to
|
||||
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#import <Foundation/NSString.h>
|
||||
|
||||
#import "SOGoEmptyAuthenticator.h"
|
||||
|
||||
@implementation SOGoEmptyAuthenticator
|
||||
|
||||
+ (id) sharedSOGoEmptyAuthenticator
|
||||
{
|
||||
static SOGoEmptyAuthenticator *auth = nil;
|
||||
|
||||
if (!auth)
|
||||
auth = [self new];
|
||||
|
||||
return auth;
|
||||
}
|
||||
|
||||
- (id) init
|
||||
{
|
||||
if ((self = [super init]))
|
||||
{
|
||||
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void) dealloc
|
||||
{
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -30,10 +30,10 @@
|
||||
|
||||
#import "NSString+Utilities.h"
|
||||
#import "SOGoStaticAuthenticator.h"
|
||||
#import "SOGoEmptyAuthenticator.h"
|
||||
#import "SOGoSystemDefaults.h"
|
||||
#import "SOGoUser.h"
|
||||
#import "SOGoUserManager.h"
|
||||
|
||||
#import "SOGoMailer.h"
|
||||
|
||||
//
|
||||
@@ -243,7 +243,7 @@
|
||||
NS_DURING
|
||||
{
|
||||
[client connect];
|
||||
if ([authenticationType isEqualToString: @"plain"])
|
||||
if ([authenticationType isEqualToString: @"plain"] && ![authenticator isKindOfClass: [SOGoEmptyAuthenticator class]])
|
||||
{
|
||||
/* XXX Allow static credentials by peeking at the classname */
|
||||
if ([authenticator isKindOfClass: [SOGoStaticAuthenticator class]])
|
||||
@@ -262,7 +262,7 @@
|
||||
reason: @"cannot send message:"
|
||||
@" (smtp) authentication failure"];
|
||||
}
|
||||
else if (authenticationType)
|
||||
else if (authenticationType && ![authenticator isKindOfClass: [SOGoEmptyAuthenticator class]])
|
||||
result = [NSException
|
||||
exceptionWithHTTPStatus: 500
|
||||
reason: @"cannot send message:"
|
||||
|
||||
@@ -57,9 +57,10 @@
|
||||
grace: (int *) _grace;
|
||||
|
||||
- (BOOL) changePasswordForLogin: (NSString *) login
|
||||
oldPassword: (NSString *) oldPassword
|
||||
newPassword: (NSString *) newPassword
|
||||
perr: (SOGoPasswordPolicyError *) perr;
|
||||
oldPassword: (NSString *) oldPassword
|
||||
newPassword: (NSString *) newPassword
|
||||
passwordRecovery: (BOOL) passwordRecovery
|
||||
perr: (SOGoPasswordPolicyError *) perr;
|
||||
|
||||
- (NSDictionary *) lookupContactEntry: (NSString *) theID
|
||||
inDomain: (NSString *) domain;
|
||||
|
||||
@@ -115,6 +115,10 @@
|
||||
- (int) maximumPictureSize;
|
||||
- (BOOL) easSearchInBody;
|
||||
|
||||
- (BOOL) isPasswordRecoveryEnabled;
|
||||
- (NSArray *) passwordRecoveryDomains;
|
||||
- (NSString *) JWTSecret;
|
||||
|
||||
@end
|
||||
|
||||
#endif /* SOGOSYSTEMDEFAULTS_H */
|
||||
@@ -755,4 +755,34 @@ _injectConfigurationFromFile (NSMutableDictionary *defaultsDict,
|
||||
return v;
|
||||
}
|
||||
|
||||
- (BOOL) isPasswordRecoveryEnabled
|
||||
{
|
||||
return [self boolForKey: @"SOGoPasswordRecovery"];
|
||||
}
|
||||
|
||||
- (NSArray *) passwordRecoveryDomains
|
||||
{
|
||||
static NSArray *passwordRecoveryDomains = nil;
|
||||
|
||||
if (!passwordRecoveryDomains)
|
||||
{
|
||||
passwordRecoveryDomains = [self stringArrayForKey: @"SOGoPasswordRecoveryDomains"];
|
||||
[passwordRecoveryDomains retain];
|
||||
}
|
||||
|
||||
return passwordRecoveryDomains;
|
||||
}
|
||||
|
||||
- (NSString *) JWTSecret
|
||||
{
|
||||
NSString *secret;
|
||||
|
||||
secret = [self stringForKey: @"SOGoJWTSecret"];
|
||||
|
||||
if (!secret)
|
||||
secret = @"SOGo"; // Default secret
|
||||
|
||||
return secret;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -33,6 +33,14 @@ extern NSString *SOGoWeekStartJanuary1;
|
||||
extern NSString *SOGoWeekStartFirst4DayWeek;
|
||||
extern NSString *SOGoWeekStartFirstFullWeek;
|
||||
|
||||
extern NSString *SOGoPasswordRecoveryDisabled;
|
||||
extern NSString *SOGoPasswordRecoveryQuestion;
|
||||
extern NSString *SOGoPasswordRecoveryQuestion1;
|
||||
extern NSString *SOGoPasswordRecoveryQuestion2;
|
||||
extern NSString *SOGoPasswordRecoveryQuestion3;
|
||||
extern NSString *SOGoPasswordRecoverySecondaryEmail;
|
||||
|
||||
|
||||
@interface SOGoUserDefaults : SOGoDefaultsSource
|
||||
{
|
||||
NSString *userLanguage;
|
||||
@@ -236,6 +244,16 @@ extern NSString *SOGoWeekStartFirstFullWeek;
|
||||
- (void) setContactsCategories: (NSArray *) newValues;
|
||||
- (NSArray *) contactsCategories;
|
||||
|
||||
/* Password recovery */
|
||||
- (void) setPasswordRecoveryMode: (NSString *) newValue;
|
||||
- (NSString *) passwordRecoveryMode;
|
||||
- (void) setPasswordRecoveryQuestion: (NSString *) newValue;
|
||||
- (NSString *) passwordRecoveryQuestion;
|
||||
- (void) setPasswordRecoveryQuestionAnswer: (NSString *) newValue;
|
||||
- (NSString *) passwordRecoveryQuestionAnswer;
|
||||
- (void) setPasswordRecoverySecondaryEmail: (NSString *) newValue;
|
||||
- (NSString *) passwordRecoverySecondaryEmail;
|
||||
|
||||
@end
|
||||
|
||||
#endif /* SOGOUSERDEFAULTS_H */
|
||||
@@ -39,6 +39,14 @@ NSString *SOGoWeekStartJanuary1 = @"January1";
|
||||
NSString *SOGoWeekStartFirst4DayWeek = @"First4DayWeek";
|
||||
NSString *SOGoWeekStartFirstFullWeek = @"FirstFullWeek";
|
||||
|
||||
NSString *SOGoPasswordRecoveryDisabled = @"Disabled";
|
||||
NSString *SOGoPasswordRecoveryQuestion = @"SecretQuestion";
|
||||
NSString *SOGoPasswordRecoveryQuestion1 = @"SecretQuestion1";
|
||||
NSString *SOGoPasswordRecoveryQuestion2 = @"SecretQuestion2";
|
||||
NSString *SOGoPasswordRecoveryQuestion3 = @"SecretQuestion3";
|
||||
NSString *SOGoPasswordRecoverySecondaryEmail = @"SecondaryEmail";
|
||||
|
||||
|
||||
@implementation SOGoUserDefaults
|
||||
|
||||
+ (NSString *) userProfileClassName
|
||||
@@ -941,4 +949,44 @@ NSString *SOGoWeekStartFirstFullWeek = @"FirstFullWeek";
|
||||
return [self stringArrayForKey: @"SOGoContactsCategories"];
|
||||
}
|
||||
|
||||
- (void) setPasswordRecoveryMode: (NSString *) newValue
|
||||
{
|
||||
[self setObject: newValue forKey: @"SOGoPasswordRecoveryMode"];
|
||||
}
|
||||
|
||||
- (NSString *) passwordRecoveryMode
|
||||
{
|
||||
return [self stringForKey: @"SOGoPasswordRecoveryMode"];
|
||||
}
|
||||
|
||||
- (void) setPasswordRecoveryQuestion: (NSString *) newValue
|
||||
{
|
||||
[self setObject: newValue forKey: @"SOGoPasswordRecoveryQuestion"];
|
||||
}
|
||||
|
||||
- (NSString *) passwordRecoveryQuestion
|
||||
{
|
||||
return [self stringForKey: @"SOGoPasswordRecoveryQuestion"];
|
||||
}
|
||||
|
||||
- (void) setPasswordRecoveryQuestionAnswer: (NSString *) newValue
|
||||
{
|
||||
[self setObject: newValue forKey: @"SOGoPasswordRecoveryQuestionAnswer"];
|
||||
}
|
||||
|
||||
- (NSString *) passwordRecoveryQuestionAnswer
|
||||
{
|
||||
return [self stringForKey: @"SOGoPasswordRecoveryQuestionAnswer"];
|
||||
}
|
||||
|
||||
- (void) setPasswordRecoverySecondaryEmail: (NSString *) newValue
|
||||
{
|
||||
[self setObject: newValue forKey: @"SOGoPasswordRecoverySecondaryEmail"];
|
||||
}
|
||||
|
||||
- (NSString *) passwordRecoverySecondaryEmail
|
||||
{
|
||||
return [self stringForKey: @"SOGoPasswordRecoverySecondaryEmail"];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -99,9 +99,16 @@
|
||||
|
||||
- (BOOL) changePasswordForLogin: (NSString *) login
|
||||
inDomain: (NSString *) domain
|
||||
oldPassword: (NSString *) oldPassword
|
||||
newPassword: (NSString *) newPassword
|
||||
perr: (SOGoPasswordPolicyError *) perr;
|
||||
oldPassword: (NSString *) oldPassword
|
||||
newPassword: (NSString *) newPassword
|
||||
passwordRecovery: (BOOL) passwordRecovery
|
||||
token: (NSString *) token
|
||||
perr: (SOGoPasswordPolicyError *) perr;
|
||||
- (NSDictionary *) getPasswordRecoveryInfosForUsername: (NSString *) username domain:(NSString *) domain;
|
||||
- (NSString *) generateAndSavePasswordRecoveryTokenWithUid: (NSString *) uid username: (NSString *) username domain:(NSString *) domain;
|
||||
- (NSString *) getTokenAndCheckPasswordRecoveryDataForUsername: (NSString *) username domain:(NSString *) domain withData:(NSDictionary *) passwordRecoveryData;
|
||||
- (NSString *) getPasswordRecoveryTokenFor: (NSString *) username domain:(NSString *) domain;
|
||||
- (BOOL) isPasswordRecoveryTokenValidFor: (NSString *) token uid: (NSString *) uid;
|
||||
@end
|
||||
|
||||
#endif /* SOGOUSERMANAGER_H */
|
||||
@@ -23,6 +23,8 @@
|
||||
#import <NGExtensions/NSNull+misc.h>
|
||||
#import <NGExtensions/NSObject+Logs.h>
|
||||
|
||||
#import <NGObjWeb/WOApplication.h>
|
||||
|
||||
#import "NSArray+Utilities.h"
|
||||
#import "NSString+Utilities.h"
|
||||
#import "NSString+Crypto.h"
|
||||
@@ -32,9 +34,23 @@
|
||||
#import "SOGoUserManager.h"
|
||||
#import "SOGoCache.h"
|
||||
#import "SOGoSource.h"
|
||||
#import "SOGoUserDefaults.h"
|
||||
#import "SOGoUserSettings.h"
|
||||
#import "WOResourceManager+SOGo.h"
|
||||
#import "JWT.h"
|
||||
|
||||
static Class NSNullK;
|
||||
|
||||
static const int kJwtDurationS = 600; // 10 minutes
|
||||
static const NSString *kUidKey = @"uid";
|
||||
static const NSString *kUsernameKey = @"username";
|
||||
static const NSString *kDomainKey = @"domain";
|
||||
static const NSString *kPasswordRecoveryTokenKey = @"passwordRecoveryToken";
|
||||
static const NSString *kModeKey = @"mode";
|
||||
static const NSString *kSecretQuestionKey = @"secretQuestion";
|
||||
static const NSString *kObfuscatedSecondaryEmailKey = @"obfuscatedSecondaryEmail";
|
||||
|
||||
|
||||
@implementation SOGoUserManagerRegistry
|
||||
|
||||
+ (void) initialize
|
||||
@@ -453,7 +469,8 @@ static Class NSNullK;
|
||||
inDomain: (NSString *) domain
|
||||
oldPassword: (NSString *) oldPassword
|
||||
newPassword: (NSString *) newPassword
|
||||
perr: (SOGoPasswordPolicyError *) perr
|
||||
passwordRecovery: (BOOL) passwordRecovery
|
||||
perr: (SOGoPasswordPolicyError *) perr
|
||||
{
|
||||
NSObject <SOGoSource> *sogoSource;
|
||||
NSString *currentID;
|
||||
@@ -472,6 +489,7 @@ static Class NSNullK;
|
||||
didChange = [sogoSource changePasswordForLogin: login
|
||||
oldPassword: oldPassword
|
||||
newPassword: newPassword
|
||||
passwordRecovery: passwordRecovery
|
||||
perr: perr];
|
||||
}
|
||||
|
||||
@@ -740,25 +758,45 @@ static Class NSNullK;
|
||||
inDomain: (NSString *) domain
|
||||
oldPassword: (NSString *) oldPassword
|
||||
newPassword: (NSString *) newPassword
|
||||
passwordRecovery: (BOOL) passwordRecovery
|
||||
token: (NSString *) token
|
||||
perr: (SOGoPasswordPolicyError *) perr
|
||||
{
|
||||
NSString *jsonUser, *userLogin;
|
||||
NSString *jsonUser, *userLogin, *uid, *userToken;
|
||||
NSMutableDictionary *currentUser;
|
||||
BOOL didChange;
|
||||
SOGoSystemDefaults *sd;
|
||||
NSDictionary *info;
|
||||
SOGoUserSettings *us;
|
||||
|
||||
jsonUser = [[SOGoCache sharedCache] userAttributesForLogin: login];
|
||||
currentUser = [jsonUser objectFromJSONString];
|
||||
|
||||
if ([currentUser isKindOfClass: NSNullK])
|
||||
currentUser = nil;
|
||||
|
||||
// Check session id for password recovery
|
||||
userToken = [self getPasswordRecoveryTokenFor: login domain: domain];
|
||||
info = [self contactInfosForUserWithUIDorEmail: login];
|
||||
uid = [info objectForKey: @"c_uid"];
|
||||
|
||||
if ([self _sourceChangePasswordForLogin: login
|
||||
if (passwordRecovery && (![userToken isEqualToString: token] || ![self isPasswordRecoveryTokenValidFor: userToken uid: uid])) {
|
||||
didChange = NO;
|
||||
*perr = PolicyPasswordRecoveryInvalidToken;
|
||||
} else if ([self _sourceChangePasswordForLogin: login
|
||||
inDomain: domain
|
||||
oldPassword: oldPassword
|
||||
newPassword: newPassword
|
||||
passwordRecovery: passwordRecovery
|
||||
perr: perr])
|
||||
{
|
||||
if (passwordRecovery) {
|
||||
// Reset session id
|
||||
us = [SOGoUserSettings settingsForUser: uid];
|
||||
[us removeObjectForKey: kPasswordRecoveryTokenKey];
|
||||
[us synchronize];
|
||||
}
|
||||
|
||||
didChange = YES;
|
||||
|
||||
if (!currentUser)
|
||||
@@ -1283,4 +1321,179 @@ static Class NSNullK;
|
||||
return login;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get password reovery info for specific user (mode, secret question, obfuscated recovery email)
|
||||
* @param username Username
|
||||
* @param domain Domain
|
||||
* @return User informations
|
||||
*/
|
||||
- (NSDictionary *) getPasswordRecoveryInfosForUsername: (NSString *) username domain:(NSString *) domain
|
||||
{
|
||||
SOGoUserDefaults *userDefaults;
|
||||
NSDictionary *info, *data;
|
||||
SOGoSystemDefaults *sd;
|
||||
NSString *uid, *suffix;
|
||||
NSMutableString *obfuscatedSecondaryEmail;
|
||||
NSInteger secondaryEmailLength = 0, obfuscatedSecondaryEmailLength = 0, i = 0;
|
||||
|
||||
userDefaults = nil;
|
||||
info = [self contactInfosForUserWithUIDorEmail: username];
|
||||
uid = [info objectForKey: @"c_uid"];
|
||||
|
||||
sd = [SOGoSystemDefaults sharedSystemDefaults];
|
||||
if ([sd enableDomainBasedUID]
|
||||
&& ![[info objectForKey: @"DomainLessLogin"] boolValue])
|
||||
{
|
||||
suffix = [NSString stringWithFormat: @"@%@", domain];
|
||||
|
||||
// Don't add @domain suffix if it's already there
|
||||
if (![uid hasSuffix: suffix])
|
||||
uid = [NSString stringWithFormat: @"%@%@", uid, suffix];
|
||||
|
||||
userDefaults = [SOGoUserDefaults defaultsForUser: uid
|
||||
inDomain: domain];
|
||||
} else {
|
||||
userDefaults = [SOGoUserDefaults defaultsForUser: uid
|
||||
inDomain: nil];
|
||||
}
|
||||
|
||||
if (nil != userDefaults && [[userDefaults passwordRecoveryMode] isEqualToString: SOGoPasswordRecoveryQuestion]) {
|
||||
data = [NSDictionary dictionaryWithObjectsAndKeys: [userDefaults passwordRecoveryMode], kModeKey,
|
||||
[userDefaults passwordRecoveryQuestion], kSecretQuestionKey,
|
||||
nil];
|
||||
} else if (nil != userDefaults && [[userDefaults passwordRecoveryMode] isEqualToString: SOGoPasswordRecoverySecondaryEmail]) {
|
||||
secondaryEmailLength = [[userDefaults passwordRecoverySecondaryEmail] length];
|
||||
// Obfuscate 80% of email. Compute nb character obfuscated
|
||||
obfuscatedSecondaryEmailLength = (NSUInteger)(0.8 * secondaryEmailLength);
|
||||
obfuscatedSecondaryEmail = [NSMutableString stringWithString: @"*"];
|
||||
for (i = 1 ; i < secondaryEmailLength ; i++) {
|
||||
if (i < obfuscatedSecondaryEmailLength
|
||||
&& ![[[userDefaults passwordRecoverySecondaryEmail] substringWithRange:NSMakeRange(i, 1)] isEqualToString: @"@"]) {
|
||||
[obfuscatedSecondaryEmail appendString: @"*"];
|
||||
} else {
|
||||
[obfuscatedSecondaryEmail appendString: [[userDefaults passwordRecoverySecondaryEmail] substringWithRange:NSMakeRange(i, 1)]];
|
||||
}
|
||||
}
|
||||
|
||||
data = [NSDictionary dictionaryWithObjectsAndKeys: [userDefaults passwordRecoveryMode], kModeKey, obfuscatedSecondaryEmail, kObfuscatedSecondaryEmailKey, nil];
|
||||
} else {
|
||||
data = [NSDictionary dictionaryWithObject: SOGoPasswordRecoveryDisabled forKey: kModeKey];
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* (Email recovery) Generates and save in user pref the JWT token
|
||||
* @param uid User id
|
||||
* @param username Username
|
||||
* @param domain Domain
|
||||
* @return JWT Token
|
||||
*/
|
||||
- (NSString *) generateAndSavePasswordRecoveryTokenWithUid: (NSString *) uid username: (NSString *) username domain:(NSString *) domain
|
||||
{
|
||||
NSString *jwtToken;
|
||||
SOGoUserSettings *us;
|
||||
|
||||
us = [SOGoUserSettings settingsForUser: uid];
|
||||
jwtToken = [[JWT sharedInstance] getJWTWithData:
|
||||
[NSDictionary dictionaryWithObjectsAndKeys: uid, kUidKey, username, kUsernameKey, domain, kDomainKey, nil]
|
||||
andValidity: kJwtDurationS];
|
||||
|
||||
[us setObject: jwtToken forKey: kPasswordRecoveryTokenKey];
|
||||
[us synchronize];
|
||||
|
||||
return jwtToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* (Secret Question) Check secret question answer, generates and save in user pref the JWT token
|
||||
* @param username Username
|
||||
* @param domain Domain
|
||||
* @param passwordRecoveryData Password recovery data (answer, ...)
|
||||
* @return JWT Token
|
||||
*/
|
||||
- (NSString *) getTokenAndCheckPasswordRecoveryDataForUsername: (NSString *) username domain:(NSString *) domain withData:(NSDictionary *) passwordRecoveryData
|
||||
{
|
||||
NSString *mode, *questionKey, *answer, *uid, *suffix, *token;
|
||||
SOGoUserDefaults *userDefaults;
|
||||
SOGoSystemDefaults *sd;
|
||||
NSDictionary *info;
|
||||
|
||||
mode = [passwordRecoveryData objectForKey:@"mode"];
|
||||
questionKey = [passwordRecoveryData objectForKey:@"question"];
|
||||
answer = [[[passwordRecoveryData objectForKey:@"answer"] lowercaseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
||||
|
||||
token = nil;
|
||||
userDefaults = nil;
|
||||
info = [self contactInfosForUserWithUIDorEmail: username];
|
||||
uid = [info objectForKey: @"c_uid"];
|
||||
sd = [SOGoSystemDefaults sharedSystemDefaults];
|
||||
if ([sd enableDomainBasedUID]
|
||||
&& ![[info objectForKey: @"DomainLessLogin"] boolValue])
|
||||
{
|
||||
suffix = [NSString stringWithFormat: @"@%@", domain];
|
||||
|
||||
// Don't add @domain suffix if it's already there
|
||||
if (![uid hasSuffix: suffix])
|
||||
uid = [NSString stringWithFormat: @"%@%@", uid, suffix];
|
||||
|
||||
userDefaults = [SOGoUserDefaults defaultsForUser: uid
|
||||
inDomain: domain];
|
||||
} else {
|
||||
userDefaults = [SOGoUserDefaults defaultsForUser: uid
|
||||
inDomain: nil];
|
||||
}
|
||||
|
||||
if ([sd isPasswordRecoveryEnabled] && nil != userDefaults && [[userDefaults passwordRecoveryMode] isEqualToString: mode]) {
|
||||
if ([[userDefaults passwordRecoveryQuestion] isEqualToString: questionKey]
|
||||
&& [[[[userDefaults passwordRecoveryQuestionAnswer] lowercaseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString: answer]) {
|
||||
token = [self generateAndSavePasswordRecoveryTokenWithUid: uid username: username domain: domain];
|
||||
}
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get token for username / domain stored in user pref
|
||||
* @param username Username
|
||||
* @param domain Domain
|
||||
* @return JWT Token
|
||||
*/
|
||||
- (NSString *) getPasswordRecoveryTokenFor: (NSString *) username domain: (NSString *) domain
|
||||
{
|
||||
NSString *uid;
|
||||
NSDictionary *info;
|
||||
SOGoUserSettings *us;
|
||||
|
||||
info = [self contactInfosForUserWithUIDorEmail: username];
|
||||
uid = [info objectForKey: @"c_uid"];
|
||||
us = [SOGoUserSettings settingsForUser: uid];
|
||||
[us synchronize];
|
||||
|
||||
return [us stringForKey: kPasswordRecoveryTokenKey];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if JWT token is valid
|
||||
* @param token Username
|
||||
* @param uid User id
|
||||
* @return True or False
|
||||
*/
|
||||
- (BOOL) isPasswordRecoveryTokenValidFor: (NSString *) token uid: (NSString *) uid {
|
||||
BOOL isJWTValid, isJWTExpired, r;
|
||||
NSDictionary *results;
|
||||
|
||||
r = NO;
|
||||
results = [[JWT sharedInstance] getDataWithJWT: token andValidity: &isJWTValid isExpired: &isJWTExpired];
|
||||
if (isJWTValid && !isJWTExpired
|
||||
&& [results objectForKey: kUidKey]
|
||||
&& [[results objectForKey: kUidKey] isEqualToString: uid]) {
|
||||
r = YES;
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -317,7 +317,9 @@
|
||||
row = [channel fetchAttributes: attrs withZone: NULL];
|
||||
value = [row objectForKey: @"c_password"];
|
||||
|
||||
rc = [self _isPassword: _pwd equalTo: value];
|
||||
if (_pwd != (id)[NSNull null]) {
|
||||
rc = [self _isPassword: _pwd equalTo: value];
|
||||
}
|
||||
[channel cancelFetch];
|
||||
}
|
||||
else
|
||||
@@ -338,11 +340,13 @@
|
||||
* @param oldPassword the previous password.
|
||||
* @param newPassword the new password.
|
||||
* @param perr will be set if the new password is not conform to the policy.
|
||||
* @param passwordRecovery YES of this is password recovery, NO otherwise. If password recovery is set, old password won't be checked
|
||||
* @return YES if the password was successfully changed.
|
||||
*/
|
||||
- (BOOL) changePasswordForLogin: (NSString *) login
|
||||
oldPassword: (NSString *) oldPassword
|
||||
newPassword: (NSString *) newPassword
|
||||
passwordRecovery: (BOOL) passwordRecovery
|
||||
perr: (SOGoPasswordPolicyError *) perr
|
||||
{
|
||||
BOOL didChange, isOldPwdOk, isPolicyOk;
|
||||
@@ -362,7 +366,7 @@
|
||||
// Verify current password
|
||||
isOldPwdOk = [self checkLogin:login password:oldPassword perr:perr expire:0 grace:0];
|
||||
|
||||
if (isOldPwdOk)
|
||||
if (isOldPwdOk || passwordRecovery)
|
||||
{
|
||||
if ([_userPasswordPolicy count])
|
||||
{
|
||||
@@ -386,7 +390,7 @@
|
||||
}
|
||||
}
|
||||
|
||||
if (isOldPwdOk && isPolicyOk)
|
||||
if ((isOldPwdOk || passwordRecovery) && isPolicyOk)
|
||||
{
|
||||
// Encrypt new password
|
||||
NSString *encryptedPassword = [self _encryptPassword: newPassword];
|
||||
|
||||
Reference in new issue
Block a user