Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions OneTimePasswordExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
089ECCC427417C0400C7EAB8 /* OneTimePasswordSettings.m in Sources */ = {isa = PBXBuildFile; fileRef = 089ECCC327417C0400C7EAB8 /* OneTimePasswordSettings.m */; };
08C8E26D26BBA376006D4608 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 08C8E26C26BBA376006D4608 /* AppDelegate.m */; };
08C8E27026BBA376006D4608 /* SceneDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 08C8E26F26BBA376006D4608 /* SceneDelegate.m */; };
08C8E27326BBA376006D4608 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 08C8E27226BBA376006D4608 /* ViewController.m */; };
Expand All @@ -21,6 +22,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
089ECCC227417C0400C7EAB8 /* OneTimePasswordSettings.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OneTimePasswordSettings.h; sourceTree = "<group>"; };
089ECCC327417C0400C7EAB8 /* OneTimePasswordSettings.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OneTimePasswordSettings.m; sourceTree = "<group>"; };
08C8E26826BBA376006D4608 /* OneTimePasswordExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = OneTimePasswordExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
08C8E26B26BBA376006D4608 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
08C8E26C26BBA376006D4608 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -55,6 +58,15 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
089ECCC127417BDA00C7EAB8 /* Model */ = {
isa = PBXGroup;
children = (
089ECCC227417C0400C7EAB8 /* OneTimePasswordSettings.h */,
089ECCC327417C0400C7EAB8 /* OneTimePasswordSettings.m */,
);
path = Model;
sourceTree = "<group>";
};
08C8E25F26BBA376006D4608 = {
isa = PBXGroup;
children = (
Expand All @@ -76,6 +88,7 @@
08C8E26A26BBA376006D4608 /* OneTimePasswordExample */ = {
isa = PBXGroup;
children = (
089ECCC127417BDA00C7EAB8 /* Model */,
4F8B41272705F8A900CF6A1A /* SettingsTableViewCell.h */,
4F8B41282705F8CF00CF6A1A /* SettingsTableViewCell.m */,
08C8E26B26BBA376006D4608 /* AppDelegate.h */,
Expand Down Expand Up @@ -215,6 +228,7 @@
4F8B41252703520D00CF6A1A /* SettingController.m in Sources */,
08C8E27E26BBA377006D4608 /* main.m in Sources */,
08C8E27026BBA376006D4608 /* SceneDelegate.m in Sources */,
089ECCC427417C0400C7EAB8 /* OneTimePasswordSettings.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
38 changes: 38 additions & 0 deletions OneTimePasswordExample/Model/OneTimePasswordSettings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// OneTimePasswordSettings.h
// OneTimePasswordExample
//
// Created by KAWASHIMA Yoshiyuki on 2021/11/15.
//

#import <Foundation/Foundation.h>
#import "OneTimePassword.h"

NS_ASSUME_NONNULL_BEGIN

@interface OneTimePasswordSettings : NSObject

@property (nonatomic) NSData *secretData;
@property (nonatomic) NSString *name;
@property (nonatomic) NSString *issuer;
@property (nonatomic) OTPAlgorithm algorithm;
@property (nonatomic) NSUInteger digits;
@property (nonatomic) NSTimeInterval period;

+ (instancetype)sharedInstance;

- (NSString *)generateOneTimePassword;

// 設定の比較用に文字列表現を取得できるようにする
- (NSString *)algorithmString;
- (NSString *)digitsString;
- (NSString *)periodString;

// 設定の文字列から保存できるようにする
- (void)saveAlgorithmString:(NSString *)algorithmString;
- (void)saveDigitsString:(NSString *)digitsString;
- (void)savePeriodString:(NSString *)periodString;

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion-badge

save より store の方がしっくりくる

Suggested change
- (void)savePeriodString:(NSString *)periodString;
- (void)storePeriodString:(NSString *)periodString;


@end

NS_ASSUME_NONNULL_END
67 changes: 67 additions & 0 deletions OneTimePasswordExample/Model/OneTimePasswordSettings.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// OneTimePasswordSettings.m
// OneTimePasswordExample
//
// Created by KAWASHIMA Yoshiyuki on 2021/11/15.
//

#import "OneTimePasswordSettings.h"
#import <Base32/MF_Base32Additions.h>

@implementation OneTimePasswordSettings

+ (instancetype)sharedInstance {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memo-badge

シングルトンにするための処理です。

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ykws
今回はマルチスレッドな実装を行なっていないため、初期化処理に@synchronizedは必要ないのでしょうか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NMai-source とても良い指摘です。

結論としては必要ないです。

@synchronized の代わりに dispatch_once を利用しています。
iOS では、これによってシングルトンを実現できるようになっています。

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ykws
承知致しました。
教えて頂きありがとうございます!
URLも参考になりました。

static OneTimePasswordSettings *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NMai-source dispatch_once を利用しているのはこの部分です。

sharedInstance = [[OneTimePasswordSettings alloc] init];

NSString *name = @"...";
NSString *issuer = @"...";
NSString *secretString = @"...";

NSData *secretData = [NSData dataWithBase32String:secretString];

sharedInstance.secretData = secretData;
sharedInstance.name = name;
sharedInstance.issuer = issuer;
sharedInstance.algorithm = [OTPToken defaultAlgorithm];
sharedInstance.digits = [OTPToken defaultDigits];
sharedInstance.period = [OTPToken defaultPeriod];

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memo-badge

デフォルト値で初期化するようにしました #19

});
return sharedInstance;
}

- (NSString *)generateOneTimePassword {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memo-badge

ワンタイムパスワード生成はここに移動しました。

OTPToken *token = [OTPToken tokenWithType:OTPTokenTypeTimer secret:self.secretData name:self.name issuer:self.issuer];
token.algorithm = self.algorithm;
token.digits = self.digits;
token.period = self.period;
return token.password;
}

- (NSString *)algorithmString {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next-badge

以下のコンバートの責務は Converter に切り離して、ここではそれを呼ぶだけにしても良さそう

  • algorithmString: OTPAlgorithm -> NSString
  • digitsString: NSUInteger -> NSString
  • periodString: NSTimeInterval -> NSString
  • saveAlgorithmString: NSString -> OTPAlgorithm
  • saveDigitsString: NSString -> NSUInteger
  • savePeriodString: NSString -> NSTimeInterval

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#21 にて対応予定

return [NSString stringForAlgorithm:self.algorithm];
}

- (NSString *)digitsString {
return [NSString stringWithFormat:@"%d", (int)self.digits];

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memo-badge

int のキャストは雑ですが、メニューでの設定値が限定されているので問題ないもの考えています。

}

- (NSString *)periodString {
return [NSString stringWithFormat:@"%d", (int)self.period];
}

- (void)saveAlgorithmString:(NSString *)algorithmString {
self.algorithm = [algorithmString algorithmValue];
}

- (void)saveDigitsString:(NSString *)digitsString {
self.digits = [digitsString intValue];
}

- (void)savePeriodString:(NSString *)periodString {
self.period = [periodString intValue];
}

@end
83 changes: 57 additions & 26 deletions OneTimePasswordExample/SettingController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

#import "SettingController.h"
#import "SettingsTableViewCell.h"
#import "ViewController.h"

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memo-badge

Model/OneTimePasswordSettings を経由してデータの共有をしているため、 ViewController への依存をなくしました。

#import "OneTimePasswordSettings.h"

#pragma mark - Settings item

typedef NS_ENUM(UInt8, SettingsItem) {
AlgorithmItem,
DigitsItem,
PeriodItme,
PeriodItem,

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memo-badge

Typo があったので修正しました。

};

extern SettingsItem SettingsItemUnknown;
Expand All @@ -24,6 +24,8 @@ typedef NS_ENUM(UInt8, SettingsItem) {

@interface SettingController () <UIPickerViewDataSource, UIPickerViewDelegate>

@property (nonatomic) OneTimePasswordSettings *settings;

@property (nonatomic) SettingsItem settingsItem;

@property (nonatomic) UIPickerView *pickerView;
Expand All @@ -32,10 +34,6 @@ @interface SettingController () <UIPickerViewDataSource, UIPickerViewDelegate>
@property (nonatomic) NSArray *digitsPatterns;
@property (nonatomic) NSArray *periodPatterns;

@property (nonatomic) NSString *algorithm;
@property (nonatomic) NSString *digits;
@property (nonatomic) NSString *period;

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memo-badge

Model/OneTimePasswordSettings を利用するようになったので不要になりました。


@end

@implementation SettingController {
Expand All @@ -45,6 +43,8 @@ @implementation SettingController {
- (void)viewDidLoad {
[super viewDidLoad];

self.settings = [OneTimePasswordSettings sharedInstance];

self.pickerView = [[UIPickerView alloc] init];
self.pickerView.center = self.view.center;
self.pickerView.dataSource = self;
Expand All @@ -54,10 +54,6 @@ - (void)viewDidLoad {
self.algorithmPatterns = @[@"SHA1", @"SHA256", @"SHA512"];
self.digitsPatterns = @[@"4", @"5", @"6", @"7", @"8", @"9", @"10"];
self.periodPatterns = @[@"30", @"60"];

self.algorithm = self.algorithmPatterns[0];
self.digits = self.digitsPatterns[0];
self.period = self.periodPatterns[0];
}

#pragma mark - Table view data source
Expand All @@ -76,15 +72,15 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
switch (indexPath.row) {
case 0:
cell.titleLabel.text = @"アルゴリズム";
cell.valueLabel.text = self.algorithm;
cell.valueLabel.text = [self.settings algorithmString];
break;
case 1:
cell.titleLabel.text = @"OTP桁数";
cell.valueLabel.text = self.digits;
cell.valueLabel.text = [self.settings digitsString];
break;
case 2:
cell.titleLabel.text = @"タイムステップ数";
cell.valueLabel.text = self.period;
cell.valueLabel.text = [self.settings periodString];
break;
}

Expand All @@ -94,8 +90,10 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// FIXME: タップしても反応しないことがある

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next-badge

設定画面の任意のセルをタップしても didSelectRowAtIndexPath が呼ばれなくて Picker の初期表示位置が意図した通りにできないことがあります。

#17 Model を追加とは別で、 TableView+Picker の課題なので、別途 Issue を作成して対応します。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#22 で対応します

self.settingsItem = indexPath.row;
[self.pickerView selectRow:0 inComponent:0 animated:true];
NSInteger selectRow = [self pickerSelectRowWithItem:self.settingsItem];
[self.pickerView selectRow:selectRow inComponent:0 animated:true];
[self.pickerView reloadAllComponents];
}

Expand All @@ -111,7 +109,7 @@ - (NSInteger)pickerView:(nonnull UIPickerView *)pickerView numberOfRowsInCompone
return self.algorithmPatterns.count;
case DigitsItem:
return self.digitsPatterns.count;
case PeriodItme:
case PeriodItem:
return self.periodPatterns.count;
default:
return 0;
Expand All @@ -126,7 +124,7 @@ - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row f
return self.algorithmPatterns[row];
case DigitsItem:
return self.digitsPatterns[row];
case PeriodItme:
case PeriodItem:
return self.periodPatterns[row];
}

Expand All @@ -136,25 +134,58 @@ - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row f
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
switch (self.settingsItem) {
case AlgorithmItem:
self.algorithm = self.algorithmPatterns[row];
[self.settings saveAlgorithmString:self.algorithmPatterns[row]];
break;
case DigitsItem:
self.digits = self.digitsPatterns[row];
[self.settings saveDigitsString:self.digitsPatterns[row]];
break;
case PeriodItme:
self.period = self.periodPatterns[row];
case PeriodItem:
[self.settings savePeriodString:self.periodPatterns[row]];
break;
}

[self.tableView reloadData];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
ViewController *viewController = segue.destinationViewController;
viewController.algorithm = self.algorithm;
viewController.digits = self.digits;
viewController.period = self.period;
#pragma mark - Settigns Index

- (NSInteger)pickerSelectRowWithItem:(SettingsItem)item{
switch (item) {
case AlgorithmItem:
return [self indexWithAlgorithmString:[self.settings algorithmString]];
case DigitsItem:
return [self indexWithDigitsString:[self.settings digitsString]];
case PeriodItem:
return [self indexWithPeriodString:[self.settings periodString]];
}
return 0;
}

- (NSInteger)indexWithAlgorithmString:(NSString *)algorithmString {
for (int i = 0; i < self.algorithmPatterns.count; i++) {
if ([self.algorithmPatterns[i] isEqual:algorithmString]) {
return i;
}
}
return 0;
}

- (NSInteger)indexWithDigitsString:(NSString *)digitsString {
for (int i = 0; i < self.digitsPatterns.count; i++) {
if ([self.digitsPatterns[i] isEqual:digitsString]) {
return i;
}
}
return 0;
}

- (NSInteger)indexWithPeriodString:(NSString *)periodString {
for (int i = 0; i < self.periodPatterns.count; i++) {
if ([self.periodPatterns[i] isEqual:periodString]) {
return i;
}
}
return 0;
}

@end
3 changes: 0 additions & 3 deletions OneTimePasswordExample/ViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (nonatomic) NSString *algorithm;
@property (nonatomic) NSString *digits;
@property (nonatomic) NSString *period;

@ykws ykws Nov 14, 2021

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memo-badge

Model/OneTimePasswordSettings を利用してデータを共有するようになったので、 property の公開は不要になりました。

フォーマットも修正 #18


@end

Loading