-
Notifications
You must be signed in to change notification settings - Fork 1
OneTimePassword を扱うための Model を追加 #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END | ||
| 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 { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ykws
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @NMai-source とても良い指摘です。 結論としては必要ないです。
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ykws |
||
| static OneTimePasswordSettings *sharedInstance = nil; | ||
| static dispatch_once_t onceToken; | ||
| dispatch_once(&onceToken, ^{ | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @NMai-source |
||
| 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]; | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. デフォルト値で初期化するようにしました #19 |
||
| }); | ||
| return sharedInstance; | ||
| } | ||
|
|
||
| - (NSString *)generateOneTimePassword { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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 { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 以下のコンバートの責務は Converter に切り離して、ここではそれを呼ぶだけにしても良さそう
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]; | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| - (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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,14 +7,14 @@ | |
|
|
||
| #import "SettingController.h" | ||
| #import "SettingsTableViewCell.h" | ||
| #import "ViewController.h" | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| #import "OneTimePasswordSettings.h" | ||
|
|
||
| #pragma mark - Settings item | ||
|
|
||
| typedef NS_ENUM(UInt8, SettingsItem) { | ||
| AlgorithmItem, | ||
| DigitsItem, | ||
| PeriodItme, | ||
| PeriodItem, | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| }; | ||
|
|
||
| extern SettingsItem SettingsItemUnknown; | ||
|
|
@@ -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; | ||
|
|
@@ -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; | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| @end | ||
|
|
||
| @implementation SettingController { | ||
|
|
@@ -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; | ||
|
|
@@ -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 | ||
|
|
@@ -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; | ||
| } | ||
|
|
||
|
|
@@ -94,8 +90,10 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N | |
| #pragma mark - Table view delegate | ||
|
|
||
| - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { | ||
| // FIXME: タップしても反応しないことがある | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 設定画面の任意のセルをタップしても #17 Model を追加とは別で、 TableView+Picker の課題なので、別途 Issue を作成して対応します。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]; | ||
| } | ||
|
|
||
|
|
@@ -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; | ||
|
|
@@ -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]; | ||
| } | ||
|
|
||
|
|
@@ -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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,9 +8,6 @@ | |
| #import <UIKit/UIKit.h> | ||
|
|
||
| @interface ViewController : UIViewController | ||
| @property (nonatomic) NSString *algorithm; | ||
| @property (nonatomic) NSString *digits; | ||
| @property (nonatomic) NSString *period; | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| @end | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
save より store の方がしっくりくる